2017-01-02 75 views
-1

當我運行我的應用程序時,我得到一個NullPointerException,所以我無法弄清楚這裏發生了什麼。MainActivity中的java.lang.NullPointerException setOnClickListener showdialog

我MainActivity.java:

package com.example.ahmad.tapleviewmy; 
import android.app.Activity; 
import android.app.Dialog; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import com.example.ahmad.tapleviewmy.DB.Adapter; 
import com.example.ahmad.tapleviewmy.myItem.Item; 
import de.codecrafters.tableview.TableView; 
import de.codecrafters.tableview.listeners.TableDataClickListener; 
import de.codecrafters.tableview.toolkit.SimpleTableDataAdapter; 
import de.codecrafters.tableview.toolkit.SimpleTableHeaderAdapter; 

public class MainActivity extends Activity{ 
private EditText nameEditText,propeliantEditText,destEditText; 
private Button SaveBtn,ShowDialog; 
private TableView <String[]> tb; 
private TableHelper tapleHelper; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //TABLE VIEW 
    tapleHelper=new TableHelper(this); 
    tb=(TableView<String[]>)findViewById(R.id.tableview); 
    tb.setColumnCount(3); 
    tb.setHeaderBackgroundColor(Color.parseColor("#2ecc71")); 
    tb.setHeaderAdapter(new SimpleTableHeaderAdapter(this,tapleHelper.getItemProbeHeaders())); 
    tb.setDataAdapter(new SimpleTableDataAdapter(this,tapleHelper.getItemProbe())); 

    ShowDialog = (Button) findViewById(R.id.ShowDialogBtn); 
    ShowDialog.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      displayDialog(); 
     } 
    }); 

    //TABLE CLICK 
    tb.addDataClickListener(new TableDataClickListener<String[]>() { 
     @Override 
     public void onDataClicked(int rowIndex, String[] clickedData) { 
      Toast.makeText(MainActivity.this,((String[])clickedData)[1],Toast.LENGTH_SHORT).show(); 
     } 
    }); 


} 


//DISPLAY INPUT DIALOG 
private void displayDialog(){ 
    try { 
     Dialog d = new Dialog(this); 
     d.setTitle("SQLITE DATA"); 
     d.setContentView(R.layout.dialog_layout); 
     SaveBtn = (Button) findViewById(R.id.saveBtn); 
     SaveBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //INITIALIZE VIEWS 
       nameEditText = (EditText) findViewById(R.id.nameEditText); 
       propeliantEditText = (EditText) findViewById(R.id.propEditText); 
       destEditText = (EditText) findViewById(R.id.destEditText); 

       Item i = new Item(); 
       i.setName(nameEditText.getText().toString()); 
       i.setPropeliant(propeliantEditText.getText().toString()); 
       i.setDestination(destEditText.getText().toString()); 


       if (new Adapter(MainActivity.this).saveItem(i)) { 
        nameEditText.setText(""); 
        propeliantEditText.setText(""); 
        destEditText.setText(""); 
        tb.setDataAdapter(new SimpleTableDataAdapter(MainActivity.this, tapleHelper.getItemProbe())); 
       } else { 
        Toast.makeText(MainActivity.this, "Not Saved", Toast.LENGTH_SHORT).show(); 
       } 
      } 

     }); 
     //SHOW DIALOG 
     d.show(); 
    } 
    catch (NullPointerException e){ 
     e.printStackTrace(); 
    } 
} 
} 

這是試圖打開的對話框時,我的調試日誌:

java.lang.NullPointerException 
W/System.err:at com.example.ahmad.tapleviewmy.MainActivity.displayDialog(MainActivity.java:67) 
W/System.err:  at  com.example.ahmad.tapleviewmy.MainActivity.access$000(MainActivity.java:19) 
W/System.err:  at  com.example.ahmad.tapleviewmy.MainActivity$1.onClick(MainActivity.java:44) 
W/System.err:  at android.view.View.performClick(View.java:4438) 
W/System.err:  at android.view.View$PerformClick.run(View.java:18422) 
W/System.err:  at android.os.Handler.handleCallback(Handler.java:733) 
W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
W/System.err:  at android.os.Looper.loop(Looper.java:136) 
W/System.err:  at a  android.app.ActivityThread.main(ActivityThread.java:5001) 
W/System.err:  at java.lang.reflect.Method.invokeNative(Native Method) 
W/System.err:  at java.lang.reflect.Method.invoke(Method.java:515) 
W/System.err:  at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
W/System.err:  at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
W/System.err:  at dalvik.system.NativeStart.main(Native Method) 
+0

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-和如何-DO-I-FIX-IT) –

回答

0

這似乎無法找到你的按鈕,呼叫ShowDialog = (Button) findViewById(R.id.ShowDialogBtn)時也許仔細檢查一下id的匹配。

如果你看一下文檔的findViewById,它說,如果它不能找到視圖

作爲一個側面說明變量的名字應該用小寫字母例如開始返回null名字,orderNumber等請參閱: http://www.javatpoint.com/java-naming-conventions