0
我正在創建一個android應用程序。而即時通訊面臨'不幸的應用程序已停止'。我剛添加了Toast活動,但無法運行模擬器。在此之前,我只能在沒有吐司活動的情況下查看用戶界面。下面是我的java代碼。真的很感謝任何幫助我進步!不幸的是,應用程序停止了Android
package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Test extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
setupMessageButton();
}
}
public void setupMessageButton(){
Button messageButton = (Button) findViewById(R.id.homebtn);
messageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Test.this, "Clicked!", Toast.LENGTH_LONG).show();
}
});
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test, container,
false);
return rootView;
}
}
}
下面是logcat的:
D/AndroidRuntime(855): Shutting down VM
W/dalvikvm(855): threadid=1: thread exiting with uncaught exception (group=0x41465700)
E/AndroidRuntime(855): FATAL EXCEPTION: main
E/AndroidRuntime(855): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Test}: java.lang.NullPointerException
E/AndroidRuntime(855):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/AndroidRuntime(855):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/AndroidRuntime(855):at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(855): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/AndroidRuntime(855): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(855): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(855): at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(855): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(855): at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(855): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(855): Caused by: java.lang.NullPointerException
E/AndroidRuntime(855): at com.example.test.Test.setupMessageButton(Test.java:36)
E/AndroidRuntime(855): at com.example.test.Test.onCreate(Test.java:27)
E/AndroidRuntime(855): at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime(855): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
E/AndroidRuntime(855): ... 11 more
Test.java的第36行?可能是由於messageButton爲空的事實造成的。 - activity_test.xml佈局包含homebtn按鈕? –
顯示activity_test.xml – Onik
acivity_test.xml <的EditText 機器人:ID =」 @ + ID/editText1" 機器人:layout_width = 「match_parent」 機器人:layout_height = 「WRAP_CONTENT」 機器人:EMS = 「10」 機器人:的inputType = 「numberPassword」> FrameLayout –