简体   繁体   English

Android Studio错误:很遗憾,我自己的应用已停止

[英]Android Studio error: Unfortunately My Own App has stopped

hope you can help me with this small code. 希望您能帮我这个小代码。 I get the following error: 我收到以下错误:

--------- beginning of crash
   05-06 23:00:26.734    1904-1904/com.example.alexismoralesgamboa.myownapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: com.example.alexismoralesgamboa.myownapp, PID: 1904
   java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:4007)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
   Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4002)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372

       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.alexismoralesgamboa.myownapp.MainActivity.onButtonClick(MainActivity.j        ava:47)

        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4002)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at       com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

This is my Main Activity java file: 这是我的主要活动java文件:

package com.example.alexismoralesgamboa.myownapp;

import android.preference.CheckBoxPreference;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;



public class MainActivity extends ActionBarActivity {
TextView textView;
CheckBox pepBox, cheeseBox;


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

    pepBox = (CheckBox) findViewById(R.id.checkBox);
    cheeseBox = (CheckBox) findViewById(R.id.checkBox2);
    textView = (TextView) findViewById(R.id.textView);

}


public void onButtonClick(final View view){
    StringBuilder str = new StringBuilder("");

    if(pepBox.isChecked()) {
       str.append("Pepperoni" + " ");
    }


    if(cheeseBox.isChecked()) {
       str.append("Extra Cheese");
    }

    if(str.length() == 0) {
        str.append("Plain");

    }
        textView.setText(str);

    }







@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


}

strong text 强文本

My manifest: 我的清单:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
    </activity>
</application>

And this is my activity main xml: 这是我的活动主要xml:

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pepperoni"
    android:id="@+id/checkBox"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Extra Cheese"
    android:id="@+id/checkBox2"
    android:layout_below="@+id/checkBox"
    android:layout_centerHorizontal="true"
    android:checked="false" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show"
    android:id="@+id/button"
    android:layout_below="@+id/checkBox2"
    android:layout_alignRight="@+id/checkBox2"
    android:layout_alignEnd="@+id/checkBox2"
    android:onClick="onButtonClick" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Plain"
    android:id="@+id/TextView"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true" />

The problem happens when I click the button on the app. 当我单击应用程序上的按钮时,就会发生问题。

Hope you can help me! 希望你能帮我! Thanks! 谢谢!

您正在扩展ActionBarActivity。如果您不需要任何具有相同功能的功能,也可以将其更改为Activity也可以更改导入部分。通常是因为这个原因。

The problem is that you have a miss-match of the TextView naming, so textView is null when you call setText() . 问题是您的TextView命名不匹配,因此在调用setText()textView为null。

In your code: 在您的代码中:

textView = (TextView) findViewById(R.id.textView);

In your xml: 在您的xml中:

android:id="@+id/TextView"

To fix the issue, just change the xml to: 要解决此问题,只需将xml更改为:

android:id="@+id/textView"

First thing first add the intent filter to your manifest otherwise your app won't even show up in the device,add the below lines of code to your manifest 首先,将意图过滤器添加到清单中,否则您的应用甚至不会显示在设备中,请将以下代码行添加到清单中

 <activity
        android:name=".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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM