当我运行我的应用程序时,我得到一个NullPointerException,所以我无法弄清楚这里发生了什么。
我的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 :(得分:0)
在调用ShowDialog = (Button) findViewById(R.id.ShowDialogBtn)
时,似乎找不到你的按钮,可能会仔细检查id的匹配。
如果查看findViewById的文档,则表示如果无法找到视图,则返回null
作为附注,变量名称应以小写字母开头,例如firstName,orderNumber等。请参阅: http://www.javatpoint.com/java-naming-conventions