2017-04-05 146 views
1

它发射到我的设备我的应用程序被终止我创建了一个简单的应用程序,该做的唯一的事情就是打印的“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!"); 

    } 

} 

}

+2

请分享你MainActivity.java代码。可能是你尝试设置一个空文本视图的文本 – user7676575

+0

错误消息表明你没有实例化你的TextView。当你没有指定一个'机器人:id'在你的TextView的XML,我的猜测是,当你做了声明'TextView'在'MainActivity',你没有实例化它(使用'findViewById(R.id。 theTextViewId)'在你的'onCreate'中)。但是你要分享您的MainActivity中的相关位,以肯定告诉 –

+0

虽然我解决我加入了Main.Activity.java代码,其中由以下回答的家伙提供的,我已经做至极一些细微的变化的问题。 –

回答

1

按照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类别,并添加以下到您的类

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!"); 
 
     
 
    } 
 

 
}

+0

非常感谢,现在它工作正常! –

+0

只是为了下一次有个小小的建议:如果logcat的输出的第一行没有任何意义,继续阅读:)试图找到你的应用程序包的名称,然后再从那里:) – ElDuderino

+0

确定。我会在下次尝试。再次感谢你。 :) –