当我通过android studio将它发送到我的设备时,我的应用终止了

时间:2017-04-05 17:16:41

标签: android xml

我创建了一个简单的应用程序,唯一能做的就是打印“Hello world”。 当我试图在我的设备上启动它时,它停止并说“不幸的是,(我的应用程序的名称)已经停止。”我到处搜索,但我找不到解决这个问题的方法。

这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:textSize="36sp"
    android:textColor="@android:color/holo_blue_dark"
    android:textStyle="bold"
    android:layout_marginLeft="12dp"
    android:layout_marginTop="12dp"
    />


</RelativeLayout>

AndroidManifest.xml(如果这有帮助。):

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
</application>

</manifest>

logcat的:

Process: com.example.android.test, PID: 6266
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.test/com.example.android.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
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.android.test.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我希望这是帮助我解决这个问题的必要条件。如果您需要更多的翻译请告诉我。

修改

这是我的Main.Activity.java代码:

package com.example.android.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

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

    // Example of a call to a native method
    //TextView tv = (TextView) findViewById(R.id.sample_text);
    //tv.setText(stringFromJNI());
}

/**
 * A native method that is implemented by the 'native-lib' native library,
 * which is packaged with this application.
 */
public native String stringFromJNI();

public class SampleActivity extends AppCompatActivity {

    // Create the variable
    TextView mTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Other view stuff

        // Get the reference
        mTextView = (TextView) findViewById(R.id.text_view);

        // Now you can do things to the Textview like change its text
        mTextView.setText("Hello world!");

    }

}

}

1 个答案:

答案 0 :(得分:2)

根据Logcat输出,看起来您在尝试设置TextView的文本之前,请尝试此操作。 首先确保将ID字段添加到xml。

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="36sp"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"

android:"@+id/text_view"
/>

然后打开您的MainActivity类并将以下内容添加到您的班级

&#13;
&#13;
public class SampleActivity extends AppCompatActivity {

  // Create the variable 
  TextView mTextView;

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Other view stuff

        // Get the reference 
        mTextView = (TextView) findViewById(R.id.text_view);

        // Now you can do things to the Textview like change its text
        mTextView.setText("Hello World!");
        
    }

}
&#13;
&#13;
&#13;