App力关闭;无法实例化活动

时间:2012-10-31 19:04:38

标签: android android-manifest forceclose

我一直在研究和研究这个问题

  • Libs被标记为libs
  • 活动在清单
  • 中声明

这是我到目前为止所提出的,过去一小时我一直被困住。

有人请帮忙。

错误:

10-31 18:58:23.654: E/AndroidRuntime(398): FATAL EXCEPTION: main
10-31 18:58:23.654: E/AndroidRuntime(398): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mikenning.chemistrywizard/com.mikenning.chemistrywizard.EquationBalancer}: java.lang.NullPointerException
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.os.Looper.loop(Looper.java:123)
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-31 18:58:23.654: E/AndroidRuntime(398):  at java.lang.reflect.Method.invokeNative(Native Method)
10-31 18:58:23.654: E/AndroidRuntime(398):  at java.lang.reflect.Method.invoke(Method.java:507)
10-31 18:58:23.654: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-31 18:58:23.654: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-31 18:58:23.654: E/AndroidRuntime(398):  at dalvik.system.NativeStart.main(Native Method)
10-31 18:58:23.654: E/AndroidRuntime(398): Caused by: java.lang.NullPointerException
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.app.Activity.findViewById(Activity.java:1647)
10-31 18:58:23.654: E/AndroidRuntime(398):  at com.mikenning.chemistrywizard.EquationBalancer.<init>(EquationBalancer.java:13)
10-31 18:58:23.654: E/AndroidRuntime(398):  at java.lang.Class.newInstanceImpl(Native Method)
10-31 18:58:23.654: E/AndroidRuntime(398):  at java.lang.Class.newInstance(Class.java:1409)
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-31 18:58:23.654: E/AndroidRuntime(398):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
10-31 18:58:23.654: E/AndroidRuntime(398):  ... 11 more

班级档案:

public class EquationBalancer extends Activity {

    HorizontalScrollView reactants;

    EditText editText = new EditText(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.balancelayout);

        reactants = (HorizontalScrollView) findViewById(R.id.reactants);

        EditText reactantNumberField = (EditText) findViewById(R.id.reactantsNumber);
        final int reactantNom = Integer.parseInt(reactantNumberField.getText().toString());

        Button ok = (Button) findViewById(R.id.reactantsNumberOk);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i = 1; i < reactantNom; i++) {
                    editText.setHint("compound");
                    reactants.addView(editText);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.balancelayout, menu);
        return true;
    }

}

Android Manifest:

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mikenning.chemistrywizard"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:screenOrientation="portrait" >
        <activity
            android:name="EquationBalancer"
            android:label="@string/title_activity_equation_balancer" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

XML布局文件:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/reactantsHowMany"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:text="@string/reactantsHowMany"
        android:textSize="18dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/reactantsNumber"
        android:paddingTop="5dp"
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/reactantsHowMany"
        android:layout_marginLeft="60dp"
        android:inputType="number"
        android:hint="e.g. 4" />

    <Button 
        android:id="@+id/reactantsNumberOk"
        android:paddingTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/reactantsHowMany"
        android:layout_alignParentRight="true"
        android:layout_marginRight="60dp"
        android:layout_alignBottom="@id/reactantsNumber"
        android:text="Set" />

    <HorizontalScrollView
        android:id="@+id/reactants"
        android:paddingTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/reactantsNumber" >

    </HorizontalScrollView>

</RelativeLayout>

更新了类文件(针对每个建议进行了更新):

    public class EquationBalancer extends Activity {

    HorizontalScrollView reactants;
    EditText reactantNumberField;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.balancelayout);

        reactants = (HorizontalScrollView) findViewById(R.id.reactants);

        final EditText editText = new EditText(this);
        editText.setHint("compound");

        reactantNumberField = (EditText) findViewById(R.id.reactantsNumber);


        Button ok = (Button) findViewById(R.id.reactantsNumberOk);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int reactantNom = 0;
                try{
                    reactantNom = Integer.parseInt(reactantNumberField.getText().toString());
                } catch (NullPointerException e) {
                    //ignore error
                }
                if (reactantNom != 0) {
                    for (int i = 1; i < reactantNom; i++) {
                        try{
                            reactants.addView(editText);
                        } catch (Exception e) {
                            //ignore
                        }
                    }
                } else if (reactantNom == 1) {
                    try {
                        reactants.addView(editText);
                    } catch (Exception e) {
                        //ignore
                    }
                } else {
                    Toast toast = Toast.makeText(EquationBalancer.this, "Please input the number of reactants", 
                            Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.balancelayout, menu);
        return true;
    }

}

2 个答案:

答案 0 :(得分:1)

您的代码无效。这一行:

Button ok = (Button) findViewById(R.id.reactantsNumberOk);

是你的罪魁祸首。您在视图夸大之前初始化ok变量,因此oknull。您必须在ok = (Button) findViewById(R.id.reactantsNumberOk);方法之后将onCreate()移至setViewContent(),让findViewById()实际找到有用的内容。

这同样适用于以下几行:

final EditText reactantNumberField = (EditText) findViewById(R.id.reactantsNumber);
final int reactantNom = Integer.parseInt(reactantNumberField.getText().toString());
HorizontalScrollView reactants = (HorizontalScrollView) findViewById(R.id.reactants);

这些都是空的,一旦您修复上述ok初始化,您的应用仍然会崩溃。

对于全局范围内的UI元素,也没有任何意义。有效代码应如下所示:

public class EquationBalancer extends Activity {

    HorizontalScrollView reactants;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.balancelayout);

        EditText reactantNumberField = (EditText) findViewById(R.id.reactantsNumber);
        int reactantNom = Integer.parseInt(reactantNumberField.getText().toString());

        reactants = (HorizontalScrollView) findViewById(R.id.reactants);

        Button ok = (Button) findViewById(R.id.reactantsNumberOk);    
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText;
                for (int i=1; i < reactantNom; i++) {
                    editText = new EditText(this);
                    editText.setHint("compound");
                    reactants.addView(editText);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.balancelayout, menu);
        return true;
    }
}

答案 1 :(得分:1)

在使用setContentView()扩充根布局之前,无法在布局中设置对视图的引用。

 final EditText reactantNumberField;

然后在onCreate中,在setContentView()

之后
 reactantNumberField = (EditText) findViewById(R.id.reactantsNumber);