gpt4 book ai didi

java.lang.RuntimeException : Unable to start activity

转载 作者:行者123 更新时间:2023-12-01 13:22:58 25 4
gpt4 key购买 nike

当我尝试处理我的应用程序时,出现此错误消息,请帮助:

这是错误消息:

   02-21 10:46:40.551    1092-1092/com.example.myapplication3.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myapplication3.app, PID: 1092
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication3.app/com.example.myapplication3.app.MainActivity2}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.myapplication3.app.MainActivity2.onCreate(MainActivity2.java:27)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)

与此错误相关的 java 代码行是: ((Button)findViewById(R.id.monBouton)).setOnClickListener(....)

这是我的代码java:

package com.example.myapplication3.app;



public class MainActivity2 extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
((Button)findViewById(R.id.monBouton)).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {

EditText texte = ((EditText)findViewById(R.id.monEditText));

String nom = texte.getText().toString();

Toast.makeText(MainActivity2.this, nom, Toast.LENGTH_SHORT).show();
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {


getMenuInflater().inflate(R.menu.main_activity2, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
return rootView;
}
}

}

XML 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<EditText
android:id="@+id/monEditText"
android:layout_height="wrap_content"
android:hint="Taper votre nom"
android:layout_width="fill_parent">

</EditText>
<!--
Le Bouton qui permettra à l’utilisateur
de valider sa saisie
-->
<Button
android:id="@+id/monBouton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Valider"
android:layout_gravity="center_horizontal">
</Button>
</LinearLayout>

最佳答案

您将获得 NullpointerException因为您的布局文件不包含 ID 为 R.id.container 的 View (很可能是 FrameLayout )

所以这段代码会崩溃:

getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();

因为布局文件中不存在“R.id.container”。放一个FrameLayout在布局文件中使用 id“container”。例如这样:

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>

关于java.lang.RuntimeException : Unable to start activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21939327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com