在使用findViewbyId方法之前,我负责放置setContentView,但它仍然给我错误。任何人都可以解释为什么会发生这种情况。这是我的代码。
public class DispActivity extends ActionBarActivity {
public String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disp);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
message = intent.getStringExtra(MainActivity.Extra);
EditText txt = (EditText)findViewById(R.id.edit_text23);
System.out.println("check point 1");
txt.setText("Having problem here");
System.out.println("check point 2");
}
fragment_disp.xml看起来像这样。
<LinearLayout 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.wifi.DispActivity$PlaceholderFragment" >
<EditText
android:id="@+id/edit_text23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"/>
</LinearLayout>
发生的错误是
05-13 23:50:46.716: I/System.out(5713): check point 1
05-13 23:50:46.716: D/AndroidRuntime(5713): Shutting down VM
05-13 23:50:46.716: W/dalvikvm(5713): threadid=1: thread exiting with uncaught exception (group=0x419c9ba8)
05-13 23:50:46.726: E/AndroidRuntime(5713): FATAL EXCEPTION: main
05-13 23:50:46.726: E/AndroidRuntime(5713): Process: com.example.wifi, PID: 5713
05-13 23:50:46.726: E/AndroidRuntime(5713): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wifi/com.example.wifi.DispActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.os.Handler.dispatchMessage(Handler.java:102)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.os.Looper.loop(Looper.java:136)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-13 23:50:46.726: E/AndroidRuntime(5713): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 23:50:46.726: E/AndroidRuntime(5713): at java.lang.reflect.Method.invoke(Method.java:515)
05-13 23:50:46.726: E/AndroidRuntime(5713): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-13 23:50:46.726: E/AndroidRuntime(5713): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-13 23:50:46.726: E/AndroidRuntime(5713): at dalvik.system.NativeStart.main(Native Method)
05-13 23:50:46.726: E/AndroidRuntime(5713): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
05-13 23:50:46.726: E/AndroidRuntime(5713): at com.example.wifi.DispActivity$PlaceholderFragment.onCreateView(DispActivity.java:74)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.app.Activity.performStart(Activity.java:5241)
05-13 23:50:46.726: E/AndroidRuntime(5713): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
05-13 23:50:46.726: E/AndroidRuntime(5713): ... 11 more
答案 0 :(得分:2)
您正在尝试将TextView转换为EditText并导致错误。您确定已经提供了相关的全部代码吗?堆栈跟踪非常清晰,包含行号。
答案 1 :(得分:0)
此代码应放在片段PlaceholderFragment
中。
EditText txt = (EditText)findViewById(R.id.edit_text23);
System.out.println("check point 1");
txt.setText("Having problem here");
System.out.println("check point 2");
EditText
位于fragment_disp.xml
,您在布局R.layout.activity_disp
中引用了NPE
,这就是您获得activity_disp.xml
的原因。 EditText
没有@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_disp, container, false);
EditText txt = (EditText) view.findViewById(R.id.edit_text23);
System.out.println("check point 1");
txt.setText("Having problem here");
System.out.println("check point 2");
}
。
<强> EDITED 强>
{{1}}