我在 MultipleID 活动的 XML 文件中创建了一个 TextView 块,它如下所示(在相对布局中):
<TextView
android:id="@+id/multipleIDtext"
android:text="@string/multipleIDtext_val"
android:layout_width="wrap_context"
android:layout_height="Wrap_context"
/>
在 MultipleID.java 文件中,在 onCreate() 方法中,我有以下行:
TextView textin = (TextView) findViewById(R.id.multipleIDtext);
稍后,仍然在 onCreate() 方法中,我有以下行:
textin.setText(message+ikinci);
其中“message+ikinci”是一个不为空的字符串值。
这会返回 NullPointerException,我无法解决。
提前致谢
如有必要,
活动代码:
package com.example.yengeliogullariaileagaci;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MultipleID extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle b = intent.getExtras();
String kendiIsmi= b.getString("isim");
int isimkactane = b.getInt("sayi");
int arrSayi = b.getInt("arrAdamSayisi");
String message = kendiIsmi + " isminde aile agacinda " + String.valueOf(isimkactane) + " tane kisi bulunmaktadir \nLutfen hangisi oldugunuzu belirtiniz";
StringBuilder s = new StringBuilder();
TextView textin = (TextView) findViewById(R.id.multipleIDtext);
for(int i=0;i<b.size()-3;i++)
{
s.append(b.getString("arrKendiIsmi"+i)+" "+b.getString("arrAnne"+i)+" "+b.getString("arrBaba"+i)+"\n");
}
String ikinci = "\n\n"+s.toString();
//TextView text = new TextView(this);
//text.setText(message+ikinci);
//text.setTextSize(15);
//setContentView(text);
textin.setText(message+ikinci);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.multiple_id, menu);
return true;
}
}
完整的 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=".MultipleID" >
<TextView
android:id="@+id/multipleIDtext"
android:text="@string/multipleIDtext_val"
android:layout_width="wrap_context"
android:layout_height="Wrap_context"
/>
</RelativeLayout>
日志猫错误日志:
09-08 14:15:12.546: E/AndroidRuntime(16292): FATAL EXCEPTION: main
09-08 14:15:12.546: E/AndroidRuntime(16292): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yengeliogullariaileagaci/com.example.yengeliogullariaileagaci.MultipleID}: java.lang.NullPointerException
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.app.ActivityThread.access$700(ActivityThread.java:152)
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.os.Looper.loop(Looper.java:137)
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.app.ActivityThread.main(ActivityThread.java:5328)
09-08 14:15:12.546: E/AndroidRuntime(16292): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 14:15:12.546: E/AndroidRuntime(16292): at java.lang.reflect.Method.invoke(Method.java:511)
09-08 14:15:12.546: E/AndroidRuntime(16292): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-08 14:15:12.546: E/AndroidRuntime(16292): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-08 14:15:12.546: E/AndroidRuntime(16292): at dalvik.system.NativeStart.main(Native Method)
09-08 14:15:12.546: E/AndroidRuntime(16292): Caused by: java.lang.NullPointerException
09-08 14:15:12.546: E/AndroidRuntime(16292): at com.example.yengeliogullariaileagaci.MultipleID.onCreate(MultipleID.java:41)
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.app.Activity.performCreate(Activity.java:5250)
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
09-08 14:15:12.546: E/AndroidRuntime(16292): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-08 14:15:12.546: E/AndroidRuntime(16292): ... 11 more