java - 当我单击按钮时,我的第一个应用程序崩溃了?

标签 java android

我正在按照 Android 官方网站的说明创建我的第一个 Android 应用程序。当我单击按钮“发送”消息时,应用程序崩溃了。

主要 Activity :

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;
    }
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

}

第一个 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"
    android:orientation="horizontal"
    tools:context="com.example.myfirstapp.MainActivity$PlaceholderFragment" >

    <EditText android:id="@+id/edit_message"
      android:layout_weight="1"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:hint="@string/edit_message" />

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

</LinearLayout>

显示消息 Activity :

package com.example.myfirstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

         // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }


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


}

我的日志猫:

07-25 20:06:16.196: E/DatabaseUtils(819): Writing exception to parcel
07-25 20:06:16.196: E/DatabaseUtils(819): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
07-25 20:06:21.201: E/DatabaseUtils(819): Writing exception to parcel
07-25 20:06:21.201: E/DatabaseUtils(819): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
07-25 20:06:21.281: W/dalvikvm(15329): threadid=1: thread exiting with uncaught exception (group=0x41745da0)
07-25 20:06:21.281: E/AndroidRuntime(15329): FATAL EXCEPTION: main
07-25 20:06:21.281: E/AndroidRuntime(15329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
07-25 20:06:21.281: E/AndroidRuntime(15329): Caused by: java.lang.NullPointerException
07-25 20:06:46.296: V/AwesomePlayer(284): checkOffloadExceptions is true
07-25 20:06:46.316: V/AwesomePlayer(284): checkOffloadExceptions is true
07-25 20:06:46.336: V/AwesomePlayer(284): checkOffloadExceptions is true
07-25 20:06:46.996: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.066: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.136: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.206: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.277: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.347: E/ShotSingle(284): <=== Exception : metadata is NULL ===>
07-25 20:06:47.417: E/ShotSingle(284): <=== Exception : metadata is NULL ===>

logcat 更新:

07-25 20:14:47.355: E/android.os.Debug(819): !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:14:53.601: E/QSEECOMAPI:(16677): Error::Failed to open /dev/qseecom device
07-25 20:15:10.157: E/QCameraHWI_Parm(284): setPreviewFpsRange: error: FPS range (15, 30) not supported

最佳答案

你是否在manifest.xml中声明了你的Activity?

应该是这样的:

<activity android:name="YOURPATH.DisplayMessageActivity" />

它将作为 .Activity2 放置在以下位置:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package.name">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity1" android:label="@string/app_name"></activity>
        <activity android:name=".Activity2"></activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
</manifest>

关于java - 当我单击按钮时,我的第一个应用程序崩溃了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966453/

相关文章:

php - android上传PHP数据如何与MySQL数据库连接?

java - 使用 Eclipse 开始使用 Java

java - Mybatis 中没有类型处理程序异常

java - 泽西 Json 和 Pojo

java - 为什么这个 KeyEvent 不起作用?

android - 如何在微调器周围创建自定义微调器,如右侧向下三角形的边框?

android - 使用菜单按钮启动 PreferenceActivity 并使用后退按钮完成

安卓崩溃报告

android - 使用 rxjava2 时出现 Retrofit2 Unauthenticated 401 错误

java - 有效实现此 Java MouseListener 逻辑