我正在编写一个由alertdialog组成的android应用程序,我得到一个空指针异常。我无法在我的代码中找到任何异常但它显示空指针异常请告诉我我在哪里做错了
在这里,我使用从here下载的库来获取按钮的材料设计。这是我的活动
package com.developer.milanandroid;
import java.io.IOException;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.gc.materialdesign.views.ButtonFloatSmall;
import com.gc.materialdesign.widgets.SnackBar;
import com.milan.paperbuttons.signoutoptionsbuttons;
public class Modes extends Activity {
public static final String PREFS_NAME = "LoginPreferences";
Button auto,manual,adminsettings;
ActionBar action_bar;
TextView welcome_headder;
String welcome_headder_string;
ButtonFloatSmall options;
signoutoptionsbuttons signout,shutdown,restart;
Dialog sign_out_dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.modes);
action_bar = getActionBar();
action_bar.hide();
welcome_headder = (TextView)findViewById(R.id.welcome_notation);
Intent welcome_headder_intent = getIntent();
welcome_headder_string= welcome_headder_intent.getStringExtra("USERNAME");
welcome_headder.setText("Welcome, "+welcome_headder_string);
options = (ButtonFloatSmall)findViewById(R.id.options_signout);
options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Testing",Toast.LENGTH_LONG).show();
sign_out_dialog = new Dialog(Modes.this);
sign_out_dialog.setTitle("Signout options");
sign_out_dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.shutdown);
sign_out_dialog.setContentView(R.layout.signout_options);
signout = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.signout);
signout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove("logged");
editor.commit();
System.exit(0);
}
});
restart = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.restart);
restart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SnackBar(Modes.this, "This will Signout and restart the panel.Are yout sure you want to Restart the panel !","Yes",new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove("logged");
editor.commit();
try
{
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot" });
}
catch(Exception e)
{
Toast.makeText(Modes.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}).show();
}
});
shutdown = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.signout_shutdown);
shutdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SnackBar(Modes.this, "Are you sure you want to shut down the panel", "yes", new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove("logged");
editor.commit();
try
{
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot -p" });
}catch (IOException e)
{
Toast.makeText(Modes.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}).show();
}
});
}
});
sign_out_dialog.show();
auto = (Button)findViewById(R.id.button_auto);
auto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent auto = new Intent(Modes.this,Auto.class);
auto.putExtra("USERNAME",welcome_headder_string);
startActivity(auto);
}
});
manual = (Button)findViewById(R.id.button_manualmode);
manual.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent manual = new Intent(Modes.this,Manual.class);
startActivity(manual);
}
});
adminsettings = (Button)findViewById(R.id.button_Adminsettings);
savedInstanceState = getIntent().getExtras();
if(savedInstanceState!=null){
String value_username = savedInstanceState.getString("USERNAME"); //getting username key,value pairs sent from previous intents
String value_password = savedInstanceState.getString("PASSWORD"); //getting password key,value pairs sent from previous intents
if(value_username.equals("medequip") && value_password.equals("medequip")){ //Comparing username and password sent from previous intent activity to display admin settings button or not...
adminsettings.setVisibility(View.VISIBLE); //setting visibility for a button
}
else
adminsettings.setVisibility(View.GONE);
}
adminsettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent retriving_database = new Intent (Modes.this,AdminSettings.class);
startActivity(retriving_database);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.modes, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的XMl:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.developer.milanandroid.Modes" >
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#FFC107">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="MODES"
android:textColor="#FFFFFF"
android:textSize="75dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/welcome_notation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="152dp"
android:layout_toRightOf="@+id/textView1"
android:gravity="right"
android:textColor="#FFFFFF" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativelayout"
android:layout_marginTop="44dp"
android:layout_weight="1"
android:orientation="vertical"
android:padding="6dip" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:orientation="horizontal" >
<Button
android:id="@+id/button_auto"
style="@style/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/automode"
android:text="@string/automode" />
<Button
android:id="@+id/button_manualmode"
style="@style/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/manualmode"
android:text="@string/manualmode" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/buttoncalibrationmode"
style="@style/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/calibration"
android:text="@string/calibration" />
<Button
android:id="@+id/button_manualmode_2"
style="@style/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/manualmode"
android:text="@string/manualmode" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="150dp"
android:orientation="horizontal" >
<Button
android:id="@+id/button_Review"
style="@style/HomeButton"
android:layout_width="380dp"
android:layout_height="wrap_content"
android:drawableTop="@drawable/review"
android:text="@string/Review" />
<Button
android:id="@+id/button_Adminsettings"
style="@style/HomeButton"
android:layout_width="380dp"
android:layout_height="wrap_content"
android:drawableTop="@drawable/adminsettings"
android:text="@string/Adminsettings" />
</LinearLayout>
</LinearLayout>
<com.gc.materialdesign.views.ButtonFloatSmall
android:id="@+id/options_signout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="72dp"
android:layout_marginTop="128dp"
android:background="#FF8F00"
android:drawable="@drawable/usr" >
</com.gc.materialdesign.views.ButtonFloatSmall>
<!-- <com.gc.materialdesign.view.ButtonFloatSmall
android:id="@+id/options_sign_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="75dp"
android:layout_marginTop="128dp"
android:background="#FF8F00"></com.gc.materialdesign.view.ButtonFloatSmall>-->
</RelativeLayout>
这是我的自定义对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.milan.paperbuttons.signoutoptionsbuttons
android:id="@+id/signout"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
widget:paper_text="Signout"
/>
<com.milan.paperbuttons.signoutoptionsbuttons
android:id="@+id/restart"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
widget:paper_text="Signout and Restart " />s
<com.milan.paperbuttons.signoutoptionsbuttons
android:id="@+id/signout_shutdown"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/restart"
android:layout_below="@+id/restart"
android:layout_marginTop="22dp"
widget:paper_text="Signout and Shutdown" />
<com.gc.materialdesign.views.ButtonFlat
android:id="@+id/dialog_return"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/signout_shutdown"
android:layout_marginRight="96dp"
android:layout_marginTop="23dp"
android:text="@string/back"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
</RelativeLayout>
这是我的logcat:
01-02 10:22:30.070: E/AndroidRuntime(13445): FATAL EXCEPTION: main
01-02 10:22:30.070: E/AndroidRuntime(13445): Process: com.developer.milanandroid, PID: 13445
01-02 10:22:30.070: E/AndroidRuntime(13445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developer.milanandroid/com.developer.milanandroid.Modes}: java.lang.NullPointerException
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.os.Looper.loop(Looper.java:136)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-02 10:22:30.070: E/AndroidRuntime(13445): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 10:22:30.070: E/AndroidRuntime(13445): at java.lang.reflect.Method.invoke(Method.java:515)
01-02 10:22:30.070: E/AndroidRuntime(13445): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-02 10:22:30.070: E/AndroidRuntime(13445): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-02 10:22:30.070: E/AndroidRuntime(13445): at dalvik.system.NativeStart.main(Native Method)
01-02 10:22:30.070: E/AndroidRuntime(13445): Caused by: java.lang.NullPointerException
01-02 10:22:30.070: E/AndroidRuntime(13445): at com.developer.milanandroid.Modes.onCreate(Modes.java:132)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.Activity.performCreate(Activity.java:5231)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-02 10:22:30.070: E/AndroidRuntime(13445): ... 11 more
答案 0 :(得分:3)
您在onClickListener()中初始化了对话框。在它之外初始化它。那应该解决它。
另外,请不要在onCreate()中添加太多代码。尝试使用多种方法。将有助于代码可读性。
编辑: 创建一个名为setupDialog()的方法;删除选项变量的onClickListener实现中的以下行,并将这些行添加到这个新创建的方法中。
sign_out_dialog = new Dialog(Modes.this);
sign_out_dialog.setTitle("Signout options");
sign_out_dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.shutdown);
sign_out_dialog.setContentView(R.layout.signout_options);
signout = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.signout);
signout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove("logged");
editor.commit();
System.exit(0);
}
});
现在,在 options.setOnClickListener 行之前调用新创建的内容。