私はこの問題を研究し、研究してきました
- Libs は libs とラベル付けされています
- 活動はマニフェストで宣言されています
それは私がこれまでに思いついたものであり、過去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;
}
}
Android マニフェスト:
<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;
}
}