java.lang.NullPointerException和未知来源 [英] java.lang.NullPointerException and Unknown Source

查看:170
本文介绍了java.lang.NullPointerException和未知来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难解决我的代码中的某些问题.我在手机上对其进行了测试,效果很好.但是,如果我在Galaxy Tab 4上对其进行测试,则会在onCreate()方法中给出java.lang.NullPointerException和未知源.

I'm having a hard time fixing some issues with my code. I tested it on my phone and it works good. But if I test it on a Galaxy Tab 4, it gives me java.lang.NullPointerException and unknown source in the onCreate() method.

public class MainActivity extends ActionBarActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainxml);
    Button butns = (Button) findViewById(R.id.sb1);
    butns.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getApplicationContext(), Sub1.class);
            startActivity(intent);
        }
    });
    Button butnss = (Button) findViewById(R.id.sb2);
    butnss.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getApplicationContext(), Sub2.class);
            startActivity(intent);
        }
    });
    Button butsns = (Button) findViewById(R.id.sb3);
    butsns.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getApplicationContext(), Sub3.class);
            startActivity(intent);
        }
    });
    Button quiz = (Button) findViewById(R.id.quiz);
    quiz.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getApplicationContext(), QuizActivity.class);
            startActivity(intent);
        }
    });
    ImageButton butsn1s = (ImageButton) findViewById(R.id.info);
    butsn1s.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getApplicationContext(), Info.class);
            startActivity(intent);
        }
    });
    ImageButton btn = (ImageButton) findViewById(R.id.fb);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent myWebLink = new Intent(Intent.ACTION_VIEW);
            myWebLink.setData(Uri.parse("https://www.facebook.com/pages/SharpMind-software/540118499454551?ref=hl"));
            startActivity(myWebLink);
        }
    });
    ImageButton cnt = (ImageButton) findViewById(R.id.count);
    cnt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent count = new Intent(getApplicationContext(),Count.class);
            startActivity(count);
        }
    });

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.settings:
            Intent settings = new Intent(getApplicationContext(), Settings.class);
            startActivity(settings);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

以下是错误:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sharpmind.Romana_pentru_BAC_PRO/com.sharpmind.Romana_pentru_BAC_PRO.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.sharpmind.Romana_pentru_BAC_PRO.MainActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
... 11 more

谢谢. 祝你有美好的一天!

Thanks. Have a nice day!

推荐答案

感谢您的帮助,现在,如果问题再次出现,我将尝试尝试. 我找到了我的main.xml布局大资源不包含上面声明的按钮之一. 我现在看起来像个傻瓜,但这确实发生了. 再次感谢您的回答和评论.

Thanks for help, now if the problem appears again I will know what to try. I've found it. My main.xml layout large resource was not containing one of the buttons declared above. I might look like a fool now, but it happens. Thanks again for answers and comments.

这篇关于java.lang.NullPointerException和未知来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆