2014-04-14 96 views
0

我很抱歉的問題標題。NullPointerException導致應用程序立即崩潰

我的問題是,我的應用程序一旦執行操作就會立即崩潰。當我讀回錯誤時,我看到一個NullPointerException這是崩潰的原因。

的錯誤指出它發生在Main.java在線路48 這裏是我的全部Main.java

package com.fuzzyapplications.tutorial.youtube.app; 

import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 

public class Main extends ActionBarActivity { 

public static final String EXTRA_MESSAGE = "com.fuzzyapplications.practice.app.MESSAGE"; 


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

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, 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); 
} 

public void sendMessage(View view){ 
    Intent intent = new Intent(this, DisplayMessageActivity.class); 

    EditText editText = (EditText) findViewById(R.string.edit_message); 

    String message = editText.getText().toString(); 

    intent.putExtra(EXTRA_MESSAGE, message); 

    startActivity(intent); 


} 

}

而且,這裏是應該被激活活動(一次一個按鈕推

package com.fuzzyapplications.tutorial.youtube.app; 

    import android.content.Intent; 
    import android.support.v7.app.ActionBarActivity; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.widget.TextView; 

    public class DisplayMessageActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Intent intent = getIntent(); 
    String message = intent.getStringExtra(Main.EXTRA_MESSAGE); 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 
    setContentView(textView); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.display_message, 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); 
} 

} 

希望這是足夠的信息給你!我是新到android,所以我不知道應該給這個問題提供多少信息,所以如果我沒有足夠的信息,請讓我知道!再次感謝。編輯:已添加Logcat信息和佈局信息!

logcat的:

FATAL EXCEPTION: main 
    java.lang.IllegalStateException: Could not execute method of the activity 
      at android.view.View$1.onClick(View.java:3802) 
      at android.view.View.performClick(View.java:4437) 
      at android.view.View$PerformClick.run(View.java:18365) 
      at android.os.Handler.handleCallback(Handler.java:725) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:5285) 
      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:1102) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at android.view.View$1.onClick(View.java:3797) 
            at android.view.View.performClick(View.java:4437) 
            at android.view.View$PerformClick.run(View.java:18365) 
            at android.os.Handler.handleCallback(Handler.java:725) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:5285) 
            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:1102) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.fuzzyapplications.tutorial.youtube.app.Main.sendMessage(Main.java:49) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at android.view.View$1.onClick(View.java:3797) 
            at android.view.View.performClick(View.java:4437) 
            at android.view.View$PerformClick.run(View.java:18365) 
            at android.os.Handler.handleCallback(Handler.java:725) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:5285) 
            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:1102) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
            at dalvik.system.NativeStart.main(Native Method) 

佈局文件:

主營:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
> 

<EditText 
    android:id="@+id/edit_message" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:hint="@string/edit_message"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_send" 
    android:onClick="sendMessage" 
    /> 


    </LinearLayout> 

MessageSendActivity佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.fuzzyapplications.practice.app.DisplayMessageActivity"> 

<TextView 
    android:text="@string/hello_world" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

回答

1

你試圖指string,這可能不存在R.java,而不是引用的id,你在XML佈局安卓宣佈,參考:ID =「@ + id/edit_message「。它會導致NullPointerException。用途:用於

EditText editText = (EditText) findViewById(R.id.edit_message); 

代替:

EditText editText = (EditText) findViewById(R.string.edit_message);