我的应用程序将正常加载,并且能够登录。单击登录后,应用程序将冻结并崩溃,并且此错误将出现在我的IDE中。我似乎在Dashboard.java的第51行上找不到nullPointerException的来源:
"btnAccount.setOnClickListener(new View.OnClickListener()"
在构建此应用程序时,如果有帮助,请按照page中的说明进行操作。您也可以忽略与TextView Logout关联的重复代码,这是出于测试目的,我想将其保留在代码中,因为我想为所有人提供完整的文件。
这是我的Dashboard.java文件:
public class Dashboard extends AppCompatActivity {
TextView Logout;
ImageButton btnSubmitIssue;
ImageButton btnCommunity;
ImageButton btnShop;
ImageButton btnHeatMap;
ImageButton btnEncyclopedia;
@Override
protected void onCreate(Bundle savedInstanceState)
{
ImageButton btnAccount = findViewById(R.id.btnAccount);
final PopupMenu dropDownAccount = new PopupMenu(getApplicationContext(), btnAccount);
final Menu menu = dropDownAccount.getMenu();
menu.add(0,0,0,"Account Details");
menu.add(0,1,0,"Change Password");
menu.add(0,2,0,"Logout");
dropDownAccount.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId())
{
case 0:
startActivity(new Intent(Dashboard.this , placeholder.class));
return true;
case 1:
startActivity(new Intent(Dashboard.this, placeholder.class));
return true;
case 2:
Intent intent2 = new Intent(Dashboard.this, Login.class);
startActivity(intent2);
Toast.makeText(Dashboard.this," Logged out Successfully !", Toast.LENGTH_SHORT).show();
}
return false;
}
});
btnAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dropDownAccount.show();
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
btnCommunity = findViewById(R.id.btnCommunity);
btnSubmitIssue = findViewById(R.id.btnSubmitIssue);
btnShop = findViewById(R.id.btnShop);
btnHeatMap = findViewById(R.id.btnHeatMap);
btnEncyclopedia = findViewById(R.id.btnEncyclopedia);
// Will setup button Listeners once the pages for these have been created.
btnEncyclopedia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
btnHeatMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
btnSubmitIssue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
btnShop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
btnCommunity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
Logout = (TextView)findViewById(R.id.tvLogout);
Logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Dashboard.this, Login.class);
startActivity(intent);
Toast.makeText(Dashboard.this," Logged out Successfully !", Toast.LENGTH_SHORT).show();
}
});
}
}
这是我的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vanda.agrivolution, PID: 10650
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vanda.agrivolution/com.example.vanda.agrivolution.Dashboard}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.vanda.agrivolution.Dashboard.onCreate(Dashboard.java:51)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:-1)
我自己发现了该错误...有趣的是,一旦您发布问题并再看一遍,您便能够自己解决该错误。
我的错误是某些代码本应位于onCreate方法的顶部。以下行:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
低于大部分代码。一旦将其移回到onCreate方法的顶部,应用程序就会按预期工作。
这是Dashboard.java文件中我的新onCreate方法(没有其他更改):
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
ImageButton btnAccount = findViewById(R.id.btnAccount);
final PopupMenu dropDownAccount = new PopupMenu(getApplicationContext(), btnAccount);
final Menu menu = dropDownAccount.getMenu();
menu.add(0,0,0,"Account Details");
menu.add(0,1,1,"Change Password");
menu.add(0,2,2,"Logout");
dropDownAccount.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId())
{
case 0:
startActivity(new Intent(Dashboard.this , placeholder.class));
return true;
case 1:
startActivity(new Intent(Dashboard.this, placeholder.class));
return true;
case 2:
Intent intent2 = new Intent(Dashboard.this, Login.class);
startActivity(intent2);
Toast.makeText(Dashboard.this," Logged out Successfully !", Toast.LENGTH_SHORT).show();
}
return false;
}
});
btnAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dropDownAccount.show();
}
});
btnCommunity = findViewById(R.id.btnCommunity);
btnSubmitIssue = findViewById(R.id.btnSubmitIssue);
btnShop = findViewById(R.id.btnShop);
btnHeatMap = findViewById(R.id.btnHeatMap);
btnEncyclopedia = findViewById(R.id.btnEncyclopedia);
// Will setup button Listeners once the pages for these have been created.
btnEncyclopedia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
btnHeatMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
btnSubmitIssue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
btnShop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
btnCommunity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Dashboard.this, placeholder.class));
}
});
Logout = (TextView)findViewById(R.id.tvLogout);
Logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Dashboard.this, Login.class);
startActivity(intent);
Toast.makeText(Dashboard.this," Logged out Successfully !", Toast.LENGTH_SHORT).show();
}
});
}