1

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

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

到目前为止,这是我想出的,在过去的一个小时里我一直被困住。

有人请帮忙。

错误:

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

}

安卓清单:

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

}
4

2 回答 2

1

您的代码无效。这一行:

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

是你的罪魁祸首。ok您在视图膨胀之前初始化变量,因此oknull. 你必须转向ok = (Button) findViewById(R.id.reactantsNumberOk);你的onCreate()aftersetViewContent()方法,才能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;
    }
}
于 2012-10-31T19:14:32.633 回答
1

在使用 setContentView() 扩展根布局之前,您不能在布局中设置对视图的引用。

 final EditText reactantNumberField;

然后在onCreate,setContentView()之后

 reactantNumberField = (EditText) findViewById(R.id.reactantsNumber);
于 2012-10-31T19:15:53.793 回答