Java 不幸的是,应用程序已经停止了Android
我正在创建一个android应用程序。我面临着“不幸的是,应用程序已停止”。我刚刚添加了toast活动,无法运行模拟器。在此之前,我只能在没有toast活动的情况下查看UI。下面是我的java代码。非常感谢对我进步的任何帮助Java 不幸的是,应用程序已经停止了Android,java,android,Java,Android,我正在创建一个android应用程序。我面临着“不幸的是,应用程序已停止”。我刚刚添加了toast活动,无法运行模拟器。在此之前,我只能在没有toast活动的情况下查看UI。下面是我的java代码。非常感谢对我进步的任何帮助 package com.example.test; import android.support.v7.app.ActionBarActivity; import android.support.v4.app.Fragment; import android.os.Bun
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;
}
}
}
以下是日志:
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
试试这个
我猜按钮homebtn属于fragment\u 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;
}
}
Test.java的第36行?可能是由messageButton为空这一事实引起的。-activity_test.xml布局包含homebtn按钮?显示activity_test.xmlacivity_test.xml@RizalAriffin您尝试过我的ans吗?@Hariharan是的,我尝试过。现在有了一个用于FindWebyid的eror。它提到“不能对非静态对象进行静态引用…”