2016-03-31 18 views
-1

私はaddActivityアクティビティを使用してデータを入力し、それを渡してlistActivityの行を作成します。しかし何らかの理由でnullPointerExceptionが発生し、アプリケーションがクラッシュします。ここで プログラムでtableRowを追加しようとするとnullPointerExceptionが発生する

は、それが発生したコード、です:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_add); 

    Button addButton = (Button) findViewById(R.id.addBttn); 
    addButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      TableLayout tl = (TableLayout) findViewById(R.id.tableLayout); 
      TableRow tr = new TableRow(AddActivity.this.getBaseContext()); 
      tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
      TextView tv = new TextView(AddActivity.this.getBaseContext()); 
      TextView tv2 = new TextView(AddActivity.this.getBaseContext()); 
      TableLayout.LayoutParams lp = new TableLayout.LayoutParams(); 
      lp.gravity= Gravity.CENTER_HORIZONTAL; 
      tv2.setLayoutParams(lp); 
      CheckBox cb = new CheckBox(AddActivity.this.getBaseContext()); 
      TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(); 
      lp2.gravity= Gravity.RIGHT; 
      cb.setLayoutParams(lp2); 

      EditText productNameEdit = (EditText) findViewById(R.id.productNameEdit); 
      EditText amountEdit = (EditText) findViewById(R.id.amountEdit); 

      tv.setText(productNameEdit.getText().toString()); 
      tv2.setText(amountEdit.getText().toString()); 

      tr.addView(tv); 
      tr.addView(tv2); 
      tr.addView(cb); 

      tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); // null pointer exception 
     } 
    }); 

} 

activity_add.xml

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Nazwa:" 
     android:id="@+id/productNameText" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/productNameEdit" 
     android:editable="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Ilość:" 
     android:id="@+id/amountText" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/amountEdit" 
     android:editable="true" 
     android:inputType="number" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Dodaj" 
     android:id="@+id/addBttn" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Anuluj" 
     android:id="@+id/cancelBttn" /> 
</LinearLayout> 

そして、ここでのログです:

03-31 09:17:07.561 1780-1780/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.NullPointerException 
     at com.example.anna.lab2.AddActivity$1.onClick(AddActivity.java:54) 
     at android.view.View.performClick(View.java:4240) 
     at android.view.View$PerformClick.run(View.java:17721) 
     at android.os.Handler.handleCallback(Handler.java:730) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5103) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
     at dalvik.system.NativeStart.main(Native Method) 
+1

エラーログを追加してください。 –

+1

logcatの出力を表示 – Pooya

+0

また、 'activity_add'の投稿 –

答えて

0

あなたは

setContentView(R.layout.activity_add); 

を設定していると、あなたは

TableLayout tl = (TableLayout) findViewById(R.id.tableLayout); 

が、R.id.tableLayout他のファイルではなく、R.layout.activity_addであると呼んでいます、これはNullPointerExceptionを投げています。

0

レイアウトファイルにtableLayoutTableLayoutがありません。

TableLayout tl = (TableLayout) findViewById(R.id.tableLayout); 

tlがここにnullである理由です。

は、レイアウトにそれを追加してください:

<TableLayout 
     android:id="@+id/tableLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
関連する問題