运行程序并在手机上停止,因为发生了错误

时间:2022-08-26 18:57:12

This is my program it is just a basic one. I'm trying to make the Start button work. So with me pressing it , it will change the Default Text of Hello World to MP 2014. But it kept giving me error on phone. I tried removing all the buttons and textview related and it worked flawlessly.

这是我的程序,它只是一个基本的程序。我正在尝试使“开始”按钮起作用。因此,在我按下它的同时,它会将Hello World的默认文本更改为MP 2014.但它一直给我错误的电话。我尝试删除所有相关的按钮和textview,它完美无缺。

package com.example.newapp;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {
    Button button1;
        TextView txtChange;
    @Override
            protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        txtChange = (TextView) findViewById(R.id.textView1);
        button1 = (Button) findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                txtChange.setText("MP 2014");
            }
        });


    04-22 12:07:14.056: E/SELinux(17541): selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
04-22 12:07:14.336: E/AndroidRuntime(17523): FATAL EXCEPTION: main
04-22 12:07:14.336: E/AndroidRuntime(17523): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newapp/com.example.newapp.MainActivity}: java.lang.NullPointerException
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.app.ActivityThread.access$700(ActivityThread.java:168)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.os.Looper.loop(Looper.java:137)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.app.ActivityThread.main(ActivityThread.java:5493)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at java.lang.reflect.Method.invokeNative(Native Method)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at java.lang.reflect.Method.invoke(Method.java:525)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at dalvik.system.NativeStart.main(Native Method)
04-22 12:07:14.336: E/AndroidRuntime(17523): Caused by: java.lang.NullPointerException
04-22 12:07:14.336: E/AndroidRuntime(17523):    at com.example.newapp.MainActivity.onCreate(MainActivity.java:29)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.app.Activity.performCreate(Activity.java:5372)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-22 12:07:14.336: E/AndroidRuntime(17523):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
04-22 12:07:14.336: E/AndroidRuntime(17523):    ... 11 more
04-22 12:07:14.986: E/BatteryService(2399): Could not open /efs/FactoryApp/batt_cable_count
04-22 12:07:15.336: E/EnterpriseContainerManager(2399): ContainerPolicy Service is not yet ready!!!
04-22 12:07:15.346: E/EnterpriseContainerManager(2399): ContainerPolicy Service is not yet ready!!!
04-22 12:07:15.451: E/SELinux(17570): Function: selinux_android_load_priority [0], There is no sepolicy file 
04-22 12:07:15.451: E/SELinux(17570):  
04-22 12:07:15.451: E/SELinux(17570): Function: selinux_android_load_priority [1], There is no sepolicy version file 
04-22 12:07:15.451: E/SELinux(17570):  
04-22 12:07:15.451: E/SELinux(17570): Function: selinux_android_load_priority , loading version is VE=SEPF_GT-N7105_4.3_0016
04-22 12:07:15.451: E/SELinux(17570):  
04-22 12:07:15.451: E/SELinux(17570):  
04-22 12:07:15.451: E/SELinux(17570): selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
04-22 12:07:15.511: E/android.os.Debug(2399): !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error
04-22 12:07:15.866: E/Samsung TTS(17570): onIsLanguageAvailable() - lang : eng, country : USA, variant : , iResult : 1
04-22 12:07:15.866: E/Samsung TTS(17570): onLoadLanguage() - LANG_COUNTRY_AVAILABLE
04-22 12:07:15.866: E/Samsung TTS(17570): onLoadLanguage() - lang : eng, country : USA, variant : f01
04-22 12:07:16.031: E/(17570): Samsung TTS Engine - initialize() : Samsung TTS Engine (released - 201302151)
04-22 12:07:16.031: E/(17570): Samsung TTS Engine - initialize() : ro.product.manufacturer - samsung
04-22 12:07:16.206: E/Samsung TTS(17570): onIsLanguageAvailable() - lang : eng, country : USA, variant : , iResult : 1
04-22 12:07:16.206: E/Samsung TTS(17570): onIsLanguageAvailable() - lang : eng, country : USA, variant : , iResult : 1
04-22 12:07:16.206: E/Samsung TTS(17570): onLoadLanguage() - LANG_COUNTRY_AVAILABLE
04-22 12:07:16.206: E/Samsung TTS(17570): onLoadLanguage() - lang : eng, country : USA, variant : f01
04-22 12:07:16.516: E/ViewRootImpl(2399): sendUserActionEvent() mView == null
04-22 12:07:16.516: E/EnterpriseContainerManager(2399): ContainerPolicy Service is not yet ready!!!
04-22 12:07:20.421: E/Watchdog(2399): !@Sync 14478

1 个解决方案

#1


4  

This code working fine for me

这段代码对我来说很好


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

公共类MainActivity扩展Activity {

Button btnClick  = null;
TextView txtView = null;

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

btnClick = (Button) findViewById(R.id.btnClick);
txtView = (TextView) findViewById(R.id.txtView);

btnClick.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    txtView.setText("MP 204");
    }
});
}

}

the layout file

<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"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btnClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="ClickMe" />

<TextView
    android:id="@+id/txtView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnClick"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/btnClick"
    android:text="@string/hello_world" />

</RelativeLayout>

#1


4  

This code working fine for me

这段代码对我来说很好


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

公共类MainActivity扩展Activity {

Button btnClick  = null;
TextView txtView = null;

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

btnClick = (Button) findViewById(R.id.btnClick);
txtView = (TextView) findViewById(R.id.txtView);

btnClick.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    txtView.setText("MP 204");
    }
});
}

}

the layout file

<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"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btnClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="ClickMe" />

<TextView
    android:id="@+id/txtView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnClick"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/btnClick"
    android:text="@string/hello_world" />

</RelativeLayout>