我是android的初学者。我使用此代码但运行app emulator时显示异常。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnpredef = (Button) findViewById(android.R.id.button1);
btnpredef.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
progressbar = ProgressDialog.show(MainActivity.this, "","LOADING...");
progressbar.setCancelable(true);
}
});
}
来自 new OnClickListener()的调试捕获错误。
感谢帮助我。
logCat:
10-05 15:05:01.499: D/AndroidRuntime(383): Shutting down VM
10-05 15:05:01.499: W/dalvikvm(383): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-05 15:05:01.509: E/AndroidRuntime(383): FATAL EXCEPTION: main
10-05 15:05:01.509: E/AndroidRuntime(383): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app007/com.example.app007.MainActivity}: java.lang.NullPointerException
10-05 15:05:01.509: E/AndroidRuntime(383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-05 15:05:01.509: E/AndroidRuntime(383): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-05 15:05:01.509: E/AndroidRuntime(383): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-05 15:05:01.509: E/AndroidRuntime(383): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-05 15:05:01.509: E/AndroidRuntime(383): at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 15:05:01.509: E/AndroidRuntime(383): at android.os.Looper.loop(Looper.java:123)
10-05 15:05:01.509: E/AndroidRuntime(383): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-05 15:05:01.509: E/AndroidRuntime(383): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 15:05:01.509: E/AndroidRuntime(383): at java.lang.reflect.Method.invoke(Method.java:521)
10-05 15:05:01.509: E/AndroidRuntime(383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-05 15:05:01.509: E/AndroidRuntime(383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-05 15:05:01.509: E/AndroidRuntime(383): at dalvik.system.NativeStart.main(Native Method)
10-05 15:05:01.509: E/AndroidRuntime(383): Caused by: java.lang.NullPointerException
10-05 15:05:01.509: E/AndroidRuntime(383): at com.example.app007.MainActivity.onCreate(MainActivity.java:25)
10-05 15:05:01.509: E/AndroidRuntime(383): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-05 15:05:01.509: E/AndroidRuntime(383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-05 15:05:01.509: E/AndroidRuntime(383): ... 11 more
答案 0 :(得分:1)
从
中删除androidButton btnpredef = (Button) findViewById(android.R.id.button1);
并且做到了
Button btnpredef = (Button) findViewById(R.id.button1);
提出参考
ProgressDialog progressBar;
然后添加此行
progressBar = new ProgressDialog(v.getContext());
前
progressbar = ProgressDialog.show(MainActivity.this, "","LOADING...");
并在最后添加此行
progressBar.show();
这是link可以帮到你。
答案 1 :(得分:0)
只需将新View.OnClickListener()
替换为new OnClickListener()
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnpredef = (Button) findViewById(R.id.button1);
btnpredef.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
progressbar = ProgressDialog.show(MainActivity.this, "","LOADING...");
progressbar.setCancelable(true);
progressbar.show();
}
});
答案 2 :(得分:0)
public class MainActivity extends Activity{
ProgressDialog progressbar; <----
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnpredef = (Button) findViewById(android.R.id.button1);
btnpredef.setOnClickListener(OnClickListener() <---
{
@Override
public void onClick(View v)
{
progressbar = ProgressDialog.show(v.getContext(), "","LOADING..."); <--
progressbar.setCancelable(true);
}
});
}
}
答案 3 :(得分:0)
如果未正确将活动名称声明到清单文件中,则会发生此异常。 将包名称和活动名称检查到清单文件中。
答案 4 :(得分:0)
我将 android.R.id.button1 替换为 R.id.button1 谢谢@Pain