它發射到我的設備我的應用程序被終止我創建了一個簡單的應用程序,該做的唯一的事情就是打印的「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!");
}
}
}
請分享你MainActivity.java代碼。可能是你嘗試設置一個空文本視圖的文本 – user7676575
錯誤消息表明你沒有實例化你的TextView。當你沒有指定一個'機器人:id'在你的TextView的XML,我的猜測是,當你做了聲明'TextView'在'MainActivity',你沒有實例化它(使用'findViewById(R.id。 theTextViewId)'在你的'onCreate'中)。但是你要分享您的MainActivity中的相關位,以肯定告訴 –
雖然我解決我加入了Main.Activity.java代碼,其中由以下回答的傢伙提供的,我已經做至極一些細微的變化的問題。 –