我正在尝试使用静态字符串初始化我的textview,每当我启动此活动但模拟器总是强制关闭活动..任何人都可以帮助我吗?
以下是代码:
// ServiceAct.java
public class ServiceAct extends Activity
{
static String[] phoneno = {"No Number", "No Number", "No Number"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// to set the contacts value in status module
TextView v1 = (TextView) findViewById(R.id.stat_con1);
v1.setText(phoneno[0]);
//remaining code of the activity
....
}
}
这是logcat
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.service/com.example.service.ServiceAct}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.service.ServiceAct.onCreate(ServiceAct.java:31)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
这是布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:weightSum="1" android:layout_height="fill_parent" android:orientation="horizontal" android:layout_width="fill_parent" android:background="@color/background" >
<RelativeLayout android:layout_height="match_parent" android:layout_width="match_parent" android:layout_gravity="center" android:id="@+id/relativeLayout2">
<TextView android:text="No Number" android:id="@+id/stat_con1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:1)
根据您发布的代码,我可以看到两个可能的错误:
TextView v1
为null,表示布局文件没有任何标识为R.id.stat_con1
的TextView R.id.stat_con1
的视图不是TextView
,而您在投放到TextView
时失败<强>更新强>
Caused by: java.lang.NullPointerException
at com.example.service.ServiceAct.onCreate(ServiceAct.java:31)
检查第31行...该行中的某些内容为空。
答案 1 :(得分:1)
您是否尝试过清理项目?这看起来就像这种错误(因为其他一切看起来都很好)。如果您在Eclipse中开发,请转到项目 - &gt;清洁。选择所有项目。之后您可能还需要重建。如果这样做没有帮助,请尝试重新启动IDE。