当前位置: 首页 > 知识库问答 >
问题:

我的第一个应用程序按下按钮就崩溃了

施恩
2023-03-14

我多次尝试让developers.android(https://developer.android.com/training/basics/firstapp/index.html)上的MyFirstApp教程中的应用程序发挥作用。我正在Nexus4设备上测试我的代码,该设备装有Android 4.4.2

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.myfirstapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>

MainActivity.java

package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@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();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    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_main, container,
                false);
        return rootView;
    }
}

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.enter_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);
}
 }

DisplayMessageActivity.java

package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

 public class DisplayMessageActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);      
    setContentView(textView);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_message, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    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_display_message,
                container, false);
        return rootView;
    }
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.myfirstapp.MainActivity"
    tools:ignore="MergeRootFrame" >

    <EditText 
        android:id="@+id/enter_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/enter_message"/>

   <Button
       android:id="@+id/button_send"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/button_send"
       android:onClick="sendMessage"/>

</LinearLayout>
<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="com.example.myfirstapp.MainActivity$PlaceholderFragment" >

</RelativeLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myfirstapp.DisplayMessageActivity"
    tools:ignore="MergeRootFrame" />
<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="com.example.myfirstapp.DisplayMessageActivity$PlaceholderFragment" >
</RelativeLayout>
03-18 15:27:35.195: E/AndroidRuntime(12382): FATAL EXCEPTION: main
03-18 15:27:35.195: E/AndroidRuntime(12382): Process: com.example.myfirstapp, PID: 12382
03-18 15:27:35.195: E/AndroidRuntime(12382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41e7f580 #0 id=0x7f05003c}
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.os.Looper.loop(Looper.java:136)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at java.lang.reflect.Method.invoke(Method.java:515)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at dalvik.system.NativeStart.main(Native Method)
03-18 15:27:35.195: E/AndroidRuntime(12382): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41e7f580 #0 id=0x7f05003c}
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.app.Activity.performStart(Activity.java:5241)
03-18 15:27:35.195: E/AndroidRuntime(12382):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
03-18 15:27:35.195: E/AndroidRuntime(12382):    ... 11 more

如果有人能发现导致崩溃的错误,我能帮忙编辑文本,那就太好了。提前多谢。

共有1个答案

聂琨
2023-03-14

您的问题在这里(DisplayMessageActivity):

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   Intent intent = getIntent();
   String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

   TextView textView = new TextView(this);
   textView.setTextSize(40);
   textView.setText(message);      
   setContentView(textView);

   if (savedInstanceState == null) {
       getSupportFragmentManager().beginTransaction()
              .add(R.id.container, new PlaceholderFragment()).commit();
   }
}

您试图将片段添加到r.id.container中,该片段不是活动视图层次结构的一部分,因为它包含的唯一内容是textview

 类似资料:
  • 当我在设备上运行应用程序时单击增量按钮时,我的应用程序意外地强制执行这是我的activity_main.xml代码 这是我的mainactivity.java文件 这是调试报告05-23 22:14:45.695 297 36-29736/com.orton.birthdayCard E/AndroidRuntime:致命异常:主进程:com.orton.birthdayCard,PID:2973

  • E/AndroidRuntime:致命异常:主进程:com。维内特。计算器,PID:29435 java。lang.NullPointerException:尝试调用虚拟方法的布尔java。朗,布尔<代码>在此输入代码booleanValue()”在com的空对象引用上。维内特。计算器主要活动16美元。android上的onClick(MainActivity.java:177)。看法看法andr

  • 我的应用程序出了问题,按下任何一个按钮都会崩溃。 它在我的logcat中给出了这个错误: E/AndroidRuntime:致命异常:main process:com.example.admin.myapplication,PID:14892 Android.content.res.resources$NOTFoundException:String resource ID#0x3b5at and

  • 我一直在自己编程一个应用程序,但每次我点击一个按钮,它就会遇到一些问题(因为问题出在它的OnCreate方法上)。我把它缩小到我认为是什么原因,似乎是这条线:

  • 问题内容: 我一直试图在Eclipse中制作我的第一个android应用程序(一个简单的温度转换器),但是当我单击手机上的按钮时,该应用程序崩溃了。这是完整的Java代码 单击按钮时的LogCat 最后是按钮的xml 我不确定如何解决此问题,因此希望有人可以提供帮助。谢谢。 问题答案: 首先初始化您的按钮,然后将onclicklistener设置为它们 同样设置另一个按钮

  • 在这个孩子的acitivy我有一个页面有一个旋转器和一个按钮。 首先,按钮是工作的,但当我点击旋转器,应用程序将崩溃。为了解决这个问题,我必须改变 至 我在活动中得到了这个方法,但他没有找到这个方法,因为我查看了contentView=LayoutInflater.from(getParent()).Inflate(r.layout.show_add_expresse_event,null);se