当我通过android studio将应用程序启动到我的设备时,我的应用程序被终止
我创建了一个简单的应用程序,唯一能做的就是打印“Hello world”。 当我尝试在我的设备上启动它时,它会停止并说“不幸的是,(我的应用程序的名称)已停止”。我到处搜索,但找不到解决此问题的方法 以下是我的活动_main.xml:当我通过android studio将应用程序启动到我的设备时,我的应用程序被终止,android,xml,Android,Xml,我创建了一个简单的应用程序,唯一能做的就是打印“Hello world”。 当我尝试在我的设备上启动它时,它会停止并说“不幸的是,(我的应用程序的名称)已停止”。我到处搜索,但找不到解决此问题的方法 以下是我的活动_main.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too
<?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>
我希望这就是帮助我解决这个问题所需要的。如果您需要更多的折扣,请告诉我
编辑
下面是我的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!");
}
}
}根据Logcat输出,在获取对文本视图的引用之前,您似乎正在尝试设置文本视图的文本,请尝试以下操作。 首先确保将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.java代码。可能是您试图设置空TextView的文本。错误消息表明您没有实例化TextView。由于您没有在TextView的XML中指定
android:id
,我猜想,虽然您在main活动中声明了TextView
,但您没有实例化它(在onCreate
中使用findViewById(R.id.theTextViewId)
)。但是你必须分享MainActivity中的相关信息才能确定,虽然我解决了这个问题,但我添加了Main.Activity.java代码,并做了一些小改动,这些改动是由下面回答的人提供的。下面给你一点建议:如果logcat输出的第一行没有 有道理,继续阅读:)尝试找到你的应用程序包名称,然后从那里开始:)
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!");
}
}
<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"
/>