我在使用这个 textView 时遇到问题,应用程序意外停止,这是 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" >
<Button
android:id="@+button/btnAnterior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="46dp"
android:text="Anterior" />
<Button
android:id="@+button/btnProxima"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="45dp"
android:text="Próxima" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tvPiada"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</LinearLayout>
</ScrollView>
这是.java:
// Buttons
Button btnProxima = (Button) findViewById(R.button.btnProxima);
Button btnAnterior = (Button) findViewById(R.button.btnAnterior);
int piadaN;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Strings
final String piada1 = ("Piada1");
final String piada2 = ("Piada2");
/*piada2 = ("Piada2");
String piada3 = new String();
piada3 = ("Piada3");
String piada4 = new String();
piada4 = ("Piada4");*/
final TextView tvPiada = (TextView) findViewById(R.id.tvPiada);
// Evento dos botões
btnProxima.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
piadaN = piadaN + 1;
switch (piadaN) {
case 1:
tvPiada.setText(piada1);
break;
case 2:
tvPiada.setText(piada2);
break;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
这是我运行应用程序时的 logcat
11-13 19:00:47.843: E/AndroidRuntime(585): FATAL EXCEPTION: main
11-13 19:00:47.843: E/AndroidRuntime(585): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.piadas/com.example.piadas.MainActivity}: java.lang.NullPointerException
11-13 19:00:47.843: E/AndroidRuntime(585): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
11-13 19:00:47.843: E/AndroidRuntime(585): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-13 19:00:47.843: E/AndroidRuntime(585): at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-13 19:00:47.843: E/AndroidRuntime(585): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-13 19:00:47.843: E/AndroidRuntime(585): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 19:00:47.843: E/AndroidRuntime(585): at android.os.Looper.loop(Looper.java:137)
11-13 19:00:47.843: E/AndroidRuntime(585): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-13 19:00:47.843: E/AndroidRuntime(585): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:00:47.843: E/AndroidRuntime(585): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 19:00:47.843: E/AndroidRuntime(585): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-13 19:00:47.843: E/AndroidRuntime(585): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-13 19:00:47.843: E/AndroidRuntime(585): at dalvik.system.NativeStart.main(Native Method)
11-13 19:00:47.843: E/AndroidRuntime(585): Caused by: java.lang.NullPointerException
11-13 19:00:47.843: E/AndroidRuntime(585): at android.app.Activity.findViewById(Activity.java:1794)
11-13 19:00:47.843: E/AndroidRuntime(585): at com.example.piadas.MainActivity.<init>(MainActivity.java:13)
11-13 19:00:47.843: E/AndroidRuntime(585): at java.lang.Class.newInstanceImpl(Native Method)
11-13 19:00:47.843: E/AndroidRuntime(585): at java.lang.Class.newInstance(Class.java:1319)
11-13 19:00:47.843: E/AndroidRuntime(585): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-13 19:00:47.843: E/AndroidRuntime(585): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
11-13 19:00:47.843: E/AndroidRuntime(585): ... 11 more