0

我有一個活動和它的佈局。我已經在這個佈局中包含了一個視圖,這是所有活動中顯示錯誤消息的常見現象。這個常見視圖在所有活動中都工作正常,但在一個活動findviewby ID是給空此view.Have嘗試了很多,但無法跟蹤這些問題is.Please看到代碼,如果任何人都可以建議我something.Here是代碼findviewbyid給零

活動:

public class VerificationCodeActivity extends Activity { 

    TextView resend_code,other_numbers; 
    EditText vc_code1,vc_code2,vc_code3,vc_code4; 
    Button verify_code; 
    RelativeLayout error_layout; 
    TextView error_textview; 
    Button close_error_button; 
    Context ctx =this; 
    String confirmCode; 
    Bundle bundle; 
    String usernumber,countrycode,tempid; 
    ArrayList<String>other_numebr_list; 
    String accessType; 


     @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    setContentView(R.layout.verification_code_layout); 
    init(); 
    super.onCreate(savedInstanceState); 

} 
    void init() 
    { 
     resend_code = (TextView)findViewById(R.id.vc_resend_button); 
     resend_code.setPaintFlags(resend_code.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); 
     error_textview = (TextView)findViewById(R.id.error_text); 

     //here is the layout 
     error_layout=(RelativeLayout)findViewById(R.id.error_layout); 
     close_error_button= (Button)findViewById(R.id.close_button); 
     } 
    } 
    void showErrorMessage(Boolean showm,String message) 
    { 

     if(showm) 
     { 
      error_textview.setText(message); 
      CommonUtility.expand(error_layout); 
     } 
     else{ 
      CommonUtility.collapse(error_layout); 
     } 
    } 

class VerifyCode extends AsyncTask {

String response; 
Boolean askpin,iserror = false; 
String verifiedBy = "2"; 

@Override 
protected void onPostExecute(Void result) { 
    // TODO Auto-generated method stub 
    if(iserror) 
    { 
     showErrorMessage(true, response); 
    } 
    else if(askpin) 
    { 
     Intent startcreatpin = new Intent(ctx, CreatePasswordActivity.class); 
     startcreatpin.putExtra(VariableClass.Vari.VERIFI_CODE,confirmCode); 
     startcreatpin.putExtra(VariableClass.Vari.USERID,bundle.getString(VariableClass.Vari.COUNTRYCODE)+bundle.getString(VariableClass.Vari.USERID)); 
     ctx.startActivity(startcreatpin); 
    } 
    super.onPostExecute(result); 
} 

@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    bundle = getIntent().getExtras(); 
    super.onPreExecute(); 
} 

@Override 
protected Void doInBackground(Void... params) { 
    // TODO Auto-generated method stub 
    JSONObject parent,child; 

    response = Apis.getApisInstance(ctx).verifyCode(bundle.getString(VariableClass.Vari.USERID), bundle.getString(VariableClass.Vari.COUNTRYCODE), Prefs.getResellerID(ctx), confirmCode, "2"); 

    if(!response.equalsIgnoreCase("")) 
    { 
     try { 
      parent=new JSONObject(response); 
      //if response of failed show message 
      if(parent.getString(VariableClass.ResponseVariables.RESPONSE).equals(Apis.ErrorResponse)) 
      { 
       iserror = true; 
       child= parent.getJSONObject(VariableClass.ResponseVariables.RESPONSEMESSAGE); 
       response= child.getString(VariableClass.ResponseVariables.ERRORMESSAGE); 
      } 
      //if response of success send to create pin 
      if(parent.getString(VariableClass.ResponseVariables.RESPONSE).equals(Apis.SuccessResponse)) 
      { 
       //hit verifiedsignup 

       if(!tempid.equals("")) 
       { 
        response = Apis.getApisInstance(ctx).verifiedsignup(verifiedBy, Prefs.getResellerID(ctx), bundle.getString(VariableClass.Vari.TEMP_ID),Prefs.getUserTariff(ctx)); 
        if(!response.equals("")){ 
         askpin= true; 

        } 
        else 
        { 
         iserror = true; 
         response= getResources().getString(R.string.server_error); 
        } 
       } 
      } 

     } catch (JSONException e) { 
      iserror=true; 
      response=getResources().getString(R.string.parse_error); 
      e.printStackTrace(); 
     } 
    } 
    else{ 
     iserror=true; 
     response=getResources().getString(R.string.server_error); 
    } 

    return null; 
} 

}}

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background" 
    android:orientation="vertical" 
    android:padding="@dimen/padding_all" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/vc_text" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="@dimen/margin_top" 
      android:padding="1dp" 
      android:text="@string/vc_message" 
      android:textColor="@android:color/white" /> 

     <include 
      android:id="@+id/vc__em_layout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="@dimen/margin_top" 
      layout="@layout/error_message_layout" 
      android:visibility="gone" /> 

     <LinearLayout 
      android:id="@+id/vc_code_layout" 
      android:layout_width="fill_parent" 
      android:layout_height="@dimen/edit_box_height" 
      android:layout_marginTop="@dimen/margin_top" 
      android:background="@drawable/curve_corner_shape_layout" > 

      <EditText 
       android:id="@+id/vc_code1" 
       android:layout_width="fill_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       android:background="@drawable/curve_corner_shape_edittext" 
       android:gravity="center" 
       android:hint="X" 
       android:inputType="numberDecimal" 
       android:maxLength="1" 
       android:textColor="@android:color/white" 
       android:textSize="@dimen/edit_text_size" /> 

      <LinearLayout 
       android:layout_width="@dimen/partition_line_width" 
       android:layout_height="fill_parent" 
       android:background="@android:color/white" > 
      </LinearLayout> 

      <EditText 
       android:id="@+id/vc_code2" 
       android:layout_width="fill_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       android:background="@drawable/curve_corner_shape_edittext" 
       android:gravity="center" 
       android:hint="X" 
       android:inputType="numberDecimal" 
       android:maxLength="1" 
       android:textColor="@android:color/white" 
       android:textSize="@dimen/edit_text_size" /> 

      <LinearLayout 
       android:layout_width="@dimen/partition_line_width" 
       android:layout_height="fill_parent" 
       android:background="@android:color/white" > 
      </LinearLayout> 

      <EditText 
       android:id="@+id/vc_code3" 
       android:layout_width="fill_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       android:background="@drawable/curve_corner_shape_edittext" 
       android:gravity="center" 
       android:hint="X" 
       android:inputType="numberDecimal" 
       android:maxLength="1" 
       android:textColor="@android:color/white" 
       android:textSize="@dimen/edit_text_size" /> 

      <LinearLayout 
       android:layout_width="@dimen/partition_line_width" 
       android:layout_height="fill_parent" 
       android:background="@android:color/white" > 
      </LinearLayout> 

      <EditText 
       android:id="@+id/vc_code4" 
       android:layout_width="fill_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       android:background="@drawable/curve_corner_shape_edittext" 
       android:gravity="center" 
       android:hint="X" 
       android:inputType="numberDecimal" 
       android:maxLength="1" 
       android:textColor="@android:color/white" 
       android:textSize="@dimen/edit_text_size" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/options" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/vc_othernumber_option" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="@dimen/margin_top" 
       android:layout_weight=".5" 
       android:gravity="left" 
       android:padding="1dp" 
       android:text="@string/other_number" 
       android:textColor="@android:color/white" 
       android:visibility="gone" /> 

      <TextView 
       android:id="@+id/vc_resend_button" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="@dimen/margin_top" 
       android:layout_weight=".5" 
       android:gravity="right" 
       android:padding="1dp" 
       android:text="@string/resend" 
       android:textColor="@android:color/white" /> 
     </LinearLayout> 

     <Button 
      android:id="@+id/vc_verifycode_button" 
      android:layout_width="fill_parent" 
      android:layout_height="@dimen/button_height" 
      android:layout_below="@id/options" 
      android:layout_marginTop="@dimen/margin_top" 
      android:background="@drawable/button_selector" 
      android:text="@string/verify_text" 
      android:textColor="@android:color/white" 
      android:textSize="@dimen/button_text_size" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:gravity="center" > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="@dimen/margin_top" 
      android:background="@drawable/logo_s" /> 
    </LinearLayout> 

</RelativeLayout> 

佈局包括:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/error_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/curve_corner_shape_errormsg" 
    android:gravity="center_vertical" 
    android:paddingBottom="@dimen/padding_bottom_em" 
    android:paddingLeft="@dimen/padding_left_em" 
    android:paddingRight="@dimen/padding_right_em" 
    android:paddingTop="@dimen/padding_top_em" > 

    <TextView 
     android:id="@+id/error_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:gravity="left|center_vertical" 
     android:paddingRight="@dimen/right_padding_em" 
     android:text="Invalid userName" 
     android:textColor="@android:color/white" 
     android:textSize="@dimen/textsize_em" /> 

    <Button 

     android:id="@+id/close_button" 
     android:layout_width="@dimen/close_button_width" 
     android:layout_height="@dimen/close_button_height" 
     android:layout_marginTop="@dimen/close_button_margin_top" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/close" /> 

</RelativeLayout> 

方法 // priting真正 公共靜態無效的擴大(最終查看V){

Boolean b=false; 
if(v==null) 
{ 
    b=true; 
} 
Log.e("check if view is null",""+b); 
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
final int targtetHeight = v.getMeasuredHeight(); 

v.getLayoutParams().height = 0; 
v.setVisibility(View.VISIBLE); 
Animation a = new Animation() 
{ 
    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     v.getLayoutParams().height = interpolatedTime == 1 
       ? LayoutParams.WRAP_CONTENT 
         : (int)(targtetHeight * interpolatedTime); 
     v.requestLayout(); 
    } 

    @Override 
    public boolean willChangeBounds() { 
     return true; 
    } 
}; 

// 1dp/ms 
a.setDuration((int)(targtetHeight/v.getContext().getResources().getDisplayMetrics().density)); 
v.startAnimation(a); 

}

登錄:

10-07 17:44:04.430: E/AndroidRuntime(22640): java.lang.NullPointerException 
10-07 17:44:04.430: E/AndroidRuntime(22640): at commonUtilities.CommonUtility.expand(CommonUtility.java:86) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at com.phone91new.VerificationCodeActivity.showErrorMessage(VerificationCodeActivity.java:244) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at com.phone91new.VerificationCodeActivity$VerifyCode.onPostExecute(VerificationCodeActivity.java:264) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at com.phone91new.VerificationCodeActivity$VerifyCode.onPostExecute(VerificationCodeActivity.java:1) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at android.os.AsyncTask.finish(AsyncTask.java:631) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at android.os.AsyncTask.access$600(AsyncTask.java:177) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at android.os.Looper.loop(Looper.java:155) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at android.app.ActivityThread.main(ActivityThread.java:5520) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at java.lang.reflect.Method.invokeNative(Native Method) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at java.lang.reflect.Method.invoke(Method.java:511) 
10-07 17:44:04.430: E/AndroidRuntime(22640): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
+7

你不覺得這是錯誤的。 ??'的setContentView(R.layout.verification_code_layout); init(); super.onCreate(arg0);'需要交換它們 – Piyush 2014-10-07 12:26:52

+0

VerificationCodeActivity的第244行是什麼? – Pr38y 2014-10-07 12:33:03

+0

@ X'Factor嗨,已經改變了,但仍然是相同的錯誤來了 – 2014-10-07 12:36:37

回答

3

先寫超級構造函數onCreate方法如下所述在你的活動課

super.onCreate(arg0); 
setContentView(R.layout.verification_code_layout); 
init(); 

希望所以它會幫助,而在xml

<LinearLayout 
       android:id="@+id/vc__em_layout" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="@dimen/margin_top" 
       android:visibility="gone" > 

       <include layout="@layout/error_message_layout" /> 
    </LinearLayout> 
-1

在java中的超級用來調用父類的構造,所以首先需要通過

 super.onCreate(Bundle_object); 

還有一件事調用活動類的構造函數是onCreate()初始化生命週期組件,所以你需要使用

super and this 

both關鍵字作爲構造函數的第一行

1

當使用<include>標記來包含佈局時,您在include元素中指定的任何屬性會覆蓋所包含佈局的根元素的屬性。

在你的情況,

<include 
    android:id="@+id/vc__em_layout" 

覆蓋ID在

<RelativeLayout 
    android:id="@+id/error_layout" 

和視圖不能R.id.error_layout被發現。