当前位置: 首页 > 知识库问答 >
问题:

AndroidRuntime(1130):致命异常:main

秦焱
2023-03-14

请在这方面需要帮助。当我试图在模拟器上运行我的应用程序时,我的Logcat中出现了这些错误,我的UI崩溃了

logcat错误

07-04 15:54:08.453: W/dalvikvm(1130): threadid=1: thread exiting with uncaught     exception (group=0x2bd39930)
07-04 15:54:08.870: E/AndroidRuntime(1130): FATAL EXCEPTION: main
07-04 15:54:08.870: E/AndroidRuntime(1130): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
{com.example.giftfinder/com.example.giftfinder.SettingActivity}: java.lang.NullPointerException
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.os.Looper.loop(Looper.java:137)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.app.ActivityThread.main(ActivityThread.java:5039)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at java.lang.reflect.Method.invoke(Method.java:511)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at dalvik.system.NativeStart.main(Native Method)
07-04 15:54:08.870: E/AndroidRuntime(1130): Caused by: java.lang.NullPointerException
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at com.example.giftfinder.SettingActivity.<init>(SettingActivity.java:18)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at java.lang.Class.newInstanceImpl(Native Method)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at java.lang.Class.newInstance(Class.java:1319)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-04 15:54:08.870: E/AndroidRuntime(1130):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-04 15:54:08.870: E/AndroidRuntime(1130):     ... 11 more
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TableRow;

public class SettingActivity extends Activity {
    Spinner him;
    Spinner her;
    TableRow tr2;
    TableRow tr3;
    //ArrayAdapter<String> adapter;
    //private String[] mystring;
    Resources res = getResources();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        tr2=(TableRow)findViewById(R.id.tr2);
        tr3=(TableRow)findViewById(R.id.tr3);
        him=(Spinner)findViewById(R.id.himspinner);
        her=(Spinner)findViewById(R.id.herspinner);
}
public void onRadioButtonClicked(View view)
{
    boolean checked = ((RadioButton)view).isChecked();
    switch(view.getId()){
    case R.id.him:
        if(checked){
            tr2.setVisibility(View.VISIBLE);
            him.setVisibility(View.VISIBLE);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.himentries, android.R.layout.simple_spinner_item);
            him.setAdapter(adapter);
            her.setVisibility(View.INVISIBLE);
        }
        break;
    case R.id.her:
    if(checked){
        tr2.setVisibility(View.VISIBLE);
        him.setVisibility(View.VISIBLE);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.herentries, android.R.layout.simple_spinner_item);
        him.setAdapter(adapter);
        her.setVisibility(View.INVISIBLE);      
    }
    break;
    case R.id.couples:
        if(checked){
        tr2.setVisibility(View.VISIBLE);    
    }
    break;
    default:
        tr2.setVisibility(View.INVISIBLE);
    }

}

}
<

>

共有1个答案

丌官淇
2023-03-14

您已经将代码放在settingsactivity的构造函数中,这是错误的,不要在应用程序组件(如活动和服务)的构造函数中编写任何代码。在您的示例中,并不是已经创建了所有内容--resources对象尚未创建。将代码移到oncreate,它就可以正常工作了。

 类似资料: