APIレベル8のためのAndroid App Dev、カスタムNumberPicker
-
26-12-2019 - |
質問
最低限のAPIレベル8を必要とするアプリケーション用のカスタムナンバーピッカーを作成しようとしていますが、これまでこのコードを手に入れましたが、これはこのエラーを修復する方法がわかりません。
これまでのコードはこれです:
パッケージcom.example.symbol_temp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Integer counter=0;
Button add,sub;
final TextView display;
add = (Button) findViewById(R.id.plus);
sub = (Button) findViewById(R.id.minus);
display = (TextView) findViewById(R.id.showtemperature);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
display.setText( "" + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter--;
display.setText( "" + counter);
}
});
}
}
.
と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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.symbol_temp.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/showtemperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/minus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/showtemperature"
android:layout_centerHorizontal="true"
android:text="+"
android:onClick="add"/>
<Button
android:id="@+id/plus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/minus"
android:layout_below="@+id/showtemperature"
android:text="-" />
</RelativeLayout>
.
LogCATでこれを取得する:
04-14 13:31:59.222: E/AndroidRuntime(359): FATAL EXCEPTION: main
04-14 13:31:59.222: E/AndroidRuntime(359): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.symbol_temp/com.example.symbol_temp.MainActivity}: java.lang.NullPointerException
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.os.Looper.loop(Looper.java:123)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-14 13:31:59.222: E/AndroidRuntime(359): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 13:31:59.222: E/AndroidRuntime(359): at java.lang.reflect.Method.invoke(Method.java:521)
04-14 13:31:59.222: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-14 13:31:59.222: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-14 13:31:59.222: E/AndroidRuntime(359): at dalvik.system.NativeStart.main(Native Method)
04-14 13:31:59.222: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException
04-14 13:31:59.222: E/AndroidRuntime(359): at com.example.symbol_temp.MainActivity.onCreate(MainActivity.java:24)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-14 13:31:59.222: E/AndroidRuntime(359): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-14 13:31:59.222: E/AndroidRuntime(359): ... 11 more
.
私は私がいくつかのことを変更したと思います、行番号24はこれです:
add.setOnClickListener(new View.OnClickListener() {
.
あなたの助けをありがとうございました。
解決
あなたのコメントから
tools:context="com.example.symbol_temp.MainActivity$PlaceholderFragment"
.
ビューがfragment_main.xml
に属すると仮定します。
あなたは間違ったレイアウトを膨らませます。そして、あなたは間違ったレイアウトで見ることを探しています。
この
を変更しますsetContentView(R.layout.activity_main);
.
setContentView(R.layout.fragment_main);
.
この
も変更Integer counter=0;
.
int counter; // primitive data type
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter =0;
. 他のヒント
あなたのカウンタ変数はメソッド内で宣言されているため、なぜ試してみてください。
注:
この答えは、この質問をした人が投稿したコメントの回答として掲示されています。私はこれを簡潔にするために答えとして投稿しました
このメソッドを使用する
protected void onSaveInstanceState(Bundle savedInstance) {
super.onSaveInstanceState(savedInstance);
savedInstance.putInt("myCounter",counter); }
onCreate(Bundle savedInstanceState){
if(savedInstanceState != null)
counter = savedInstanceState.getInt("myCounter")}
.
バンドルクラスのオブジェクトであるSavedInstanceから、保存された値を再トリートして作成時に割り当てることができます。あなたが最初にあなたのカウンターがまだインスタンス化されていないかもしれないとあなたのアプリを実行しても例外をスローするかもしれないので、値がnullの場合は確認してください。
SavedValuesを再試行するときにも、必ずそれらを上書きしていないことを確認してください。