2015-02-23 80 views
2

當我想移動對其他活動我有這樣的例外:了java.lang.RuntimeException:無法實例活動ComponentInfo:顯示java.lang.NullPointerException

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ua.kbv.adviser/ua.kbv.adviser.AdviceWithText}: java.lang.NullPointerException 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    at android.app.ActivityThread.access$600(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5041) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
    at android.content.ContextWrapper.getResources(ContextWrapper.java:89) 
    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78) 
    at ua.kbv.adviser.AdviceWithText.<init>(AdviceWithText.java:18) 
    at java.lang.Class.newInstanceImpl(Native Method) 
    at java.lang.Class.newInstance(Class.java:1319) 
    at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    at android.app.ActivityThread.access$600(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5041) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    at dalvik.system.NativeStart.main(Native Method) 

清單:

<application android:allowBackup="true" 
    android:label="@string/app_name" 
    android:icon="@mipmap/ic_launcher" 
    android:theme="@style/AppTheme"> 

    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 
      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 

    <activity android:name=".AdviceWithText"/> 
    <activity android:name=".AdviceWithoutText"/> 
    <supports-screens 
     android:smallScreens="true" 
     android:normalScreens="true" 
     android:largeScreens="true" 
     android:anyDensity="true" 
     /> 
</application> 

主要活動:

public class MainActivity extends Activity{ 

    private static long back_pressed; 

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

    public void clickOnGetAdvice(View view){ 


     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

     builder.setTitle(R.string.builderTitle) 
       .setMessage(R.string.builderMessage) 
       .setCancelable(true) 
       .setIcon(R.mipmap.ic_action) 
       .setNeutralButton(R.string.neutralButton, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(MainActivity.this, AdviceWithText.class); 
         startActivity(intent); 
         dialog.dismiss(); 
        } 
       }) 
       .setPositiveButton(R.string.positivebutton, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(MainActivity.this, AdviceWithoutText.class); 
         startActivity(intent); 
         dialog.dismiss(); 
        } 
       }); 

     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

    @Override 
    public void onBackPressed() { 
     if (back_pressed + 2000 > System.currentTimeMillis()) { 
      super.onBackPressed(); 
     } else { 
      Toast.makeText(getBaseContext(), R.string.exitToast, Toast.LENGTH_SHORT).show(); 
     } 

     back_pressed = System.currentTimeMillis(); 

    } 

    public void clickOnAbout(View view){ 
     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
     builder.setTitle(R.string.builderAboutTitle) 
       .setMessage(R.string.builderMessageAbout) 
       .setCancelable(true) 
       .setIcon(R.mipmap.ic_action_info) 
       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 

     AlertDialog alertDialog = builder.create(); 
     alertDialog.show(); 

    } 

     @Override 
     protected void onDestroy() { 
      super.onDestroy(); 
      System.exit(0); 
     } 
} 

Recieving活動:

public class AdviceWithText extends Activity{ 

    TextView answer; 
    private EditText question; 
    String [] advices = getResources().getStringArray(R.array.advices); 

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

    public void getAdvice(View view){ 
     answer = (TextView) findViewById(R.id.answerText); 
     question = (EditText) findViewById(R.id.questionWith); 
     String text = question.getText().toString(); 
     int rand = (int) Math.floor(Math.random()* advices.length); 
     if(text.equals("")){ 
      Toast.makeText(getApplicationContext(),R.string.toastWith, Toast.LENGTH_LONG).show(); 
     } else if(text.matches("[0-9]+")) { 
      Toast.makeText(getApplicationContext(),R.string.toastWithSecond, Toast.LENGTH_LONG).show(); 
     } else { 
      answer.setText(advices[rand]); 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     finish(); 
    } 

    public void reset(View view){ 
     question = (EditText) findViewById(R.id.questionWith); 
     question.setText(""); 
     answer.setText(""); 

    } 

} 

回答

1

要調用從初始化,這是行不通的getResources()。一般來說,除非有指示,否則在super.onCreate()已返回之前,請不要撥打Activity上的方法。

在這種情況下,更換:

String [] advices = getResources().getStringArray(R.array.advices); 

有:

String [] advices; 

,並添加:

advices = getResources().getStringArray(R.array.advices); 
super.onCreate()

onCreate()方法。

+0

它幫助!非常感謝你! – Belial 2015-02-23 23:05:37

相關問題