[英]Wanting to throw an intent to open a new activity but continually crashes
這里沒有花哨或復雜的東西。 我只是想從按文本視圖時打開一個新的空白活動。 從項目完成的文件中,我似乎已經按要求完成了。 但是,每次我單擊textview來啟動打開其他活動的意圖時,它都會崩潰。 我似乎也找不到原因。 在一個單獨的應用程序中,我通過按鈕從編輯文本字段發送文本字符串,看起來工作正常,並且還有其他一些更詳細的代碼可以執行該操作,所以我不完全確定這里的錯誤發生在哪里。 。
package com.example.android.miwok;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
}
public void openNumbersList(View view) {
Intent i = new Intent(this, NumbersActivity.class);
startActivity(i);
}
}
package com.example.android.miwok;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class NumbersActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:background="@color/tan_background"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity">
<TextView
android:id="@+id/numbers"
style="@style/CategoryStyle"
android:background="@color/category_numbers"
android:onClick="openNumbersList"
android:text="@string/category_numbers" />
<TextView
android:id="@+id/family"
style="@style/CategoryStyle"
android:background="@color/category_family"
android:text="@string/category_family" />
<TextView
android:id="@+id/colors"
style="@style/CategoryStyle"
android:background="@color/category_colors"
android:text="@string/category_colors" />
<TextView
android:id="@+id/phrases"
style="@style/CategoryStyle"
android:background="@color/category_phrases"
android:text="@string/category_phrases" />
</LinearLayout>
在logcat中,當應用程序崩潰時會顯示錯誤消息:
02-20 20:10:26.762 7582-7582 / com.example.android.miwok E / AndroidRuntime:致命異常:主進程:com.example.android.miwok,PID:7582 java.lang.RuntimeException:無法啟動活動ComponentInfo {com.example.android.miwok / com.example.android.miwok.NumbersActivity}:android.view.InflateException:二進制XML文件第0行:二進制XML文件第0行:膨脹類TextView時出錯
在具有ID號的Text視圖的xml布局中添加
android:clickable="true"
僅當此屬性設置為true時,才會調用文本視圖的onclick處理程序
並為您的所有textview添加高度和寬度。
android:height="wrap_content"
android:width="wrap_content"
這一定是導致應用崩潰的原因
有關於如何設置點擊聽眾textviews后這里 。
“使類TextView膨脹的錯誤”表示您在下一個活動中的xml錯誤。 嘗試從第二個活動的布局中刪除所有視圖,仔細檢查布局,然后嘗試。 如果這只是您要確認的意圖啟動,則應該可以進行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.