[英]Unfortunately application has stopped Android
Im creating an android app. 我正在创建一个Android应用程序。 And im facing the 'Unfortunately application has stopped'. 我面临着“不幸的是,应用程序已停止”。 I just added the toast activity and i was unable to run the emulator. 我刚刚添加了吐司活动,但无法运行模拟器。 Before that i was able to view the UI only without the toast activity. 在此之前,我只能在没有敬酒活动的情况下查看UI。 Below is my java codes. 以下是我的Java代码。 Really appreciate any help for me to progress! 非常感谢任何对我进步的帮助!
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;
}
}
} }
Below is the logcat: 下面是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
Try this.. 尝试这个..
I guess Button
homebtn is belongs to fragment_test.xml
so try like below and also remove setupMessageButton()
method. 我猜Button
homebtn属于fragment_test.xml
因此请尝试如下操作,并删除setupMessageButton()
方法。
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);
Button messageButton = (Button) rootView.findViewById(R.id.homebtn);
messageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked!", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.