AndroidRuntime(1130):致命异常:主要

时间:2013-07-04 20:01:25

标签: android

请在这方面需要帮助。当我尝试在模拟器上运行我的应用程序时,我在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

这是我的SettingActivity.java文件。当我单击我的Mainactivity文件中的一个按钮时,该按钮将显示由于或错误导致实际崩溃的活动..

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);
    }

}

}

我的settings.xml文件

<

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/background"> <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/tr1"> <TextView android:id="@+id/gender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/gender" android:textSize="25sp" android:paddingLeft="20dp" android:paddingTop="10dp" android:paddingBottom="10dp" /> <RadioGroup android:id="@+id/rg1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="25dp" android:paddingLeft="20dp" android:paddingTop="15dp"> <RadioButton android:id="@+id/him" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/him" android:textSize="15sp" android:onClick="onRadioButtonClicked"/> <RadioButton android:id="@+id/her" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/her" android:textSize="15sp" android:layout_marginLeft="10dp" android:onClick="onRadioButtonClicked"/> <RadioButton android:id="@+id/couples" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/couples" android:textSize="15sp" android:layout_marginLeft="10dp" android:onClick="onRadioButtonClicked"/> </RadioGroup> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tr2" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/relation" android:textSize="25sp" android:paddingLeft="20dp" android:paddingTop="10dp"/> <Spinner android:id="@+id/himspinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="25dp" android:prompt="@string/himprompt" /> <Spinner android:id="@+id/herspinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/herentries" android:prompt="@string/herprompt" android:visibility="invisible" android:layout_marginLeft="10dp"/> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tr3" android:visibility="invisible" > <TextView android:id="@+id/category" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/category" android:textSize="25sp" android:paddingLeft="20dp" android:paddingTop="10dp"/> <RadioGroup android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="25dp" android:paddingLeft="20dp" android:paddingTop="15dp"> <RadioButton android:id="@+id/occ" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/occ" android:textSize="15sp"/> <RadioButton android:id="@+id/feel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/feel" android:textSize="15sp" android:layout_marginLeft="10dp"/> </RadioGroup> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tr4" android:visibility="invisible"> <Spinner android:id="@+id/occspinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:entries="@array/occentries" android:prompt="@string/occprompt"/> <Spinner android:id="@+id/feelspinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:entries="@array/feelentries" android:prompt="@string/feelprompt"/> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tr5" android:visibility="invisible"> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/age" android:textSize="25sp" android:paddingLeft="20dp" android:paddingTop="10dp"/> <Spinner android:id="@+id/agespinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="45dp" android:entries="@array/ageentries" android:prompt="@string/ageprompt"/> </TableRow> </TableLayout>

&GT;

1 个答案:

答案 0 :(得分:0)

您已将代码放入SettingsActivity的构造函数中,这是错误的,不要在应用程序组件的构造函数中编写任何代码(如活动和服务)。在您的情况下,并非所有内容都已创建 - Resources对象尚未创建。将代码移至onCreate代替,它将正常工作。