我正在製作一個簡單的應用程序,其中上下文視圖發生更改,然後顯示作爲用戶輸入的輸入信息。我不明白爲什麼應用程序在更改上下文視圖時不斷崩潰。方法丟失或簽名不正確
而且Android的工作室給出了這樣的警告:
法「烤麪包機」,在「FirstActivity」丟失或不正確的簽名。
這裏是我的代碼:
activity_me_clicked.xml:
<?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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/welcome"
android:textSize="36sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:text="@string/intro"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginEnd="7dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/named"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="MainProcess"
android:text="@string/done" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
tools:ignore="UselessLeaf"></LinearLayout>
</LinearLayout>
</LinearLayout>
FirstActivity.Java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class FirstActivity extends AppCompatActivity {
public static String owner_string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_clicked);
}
public void MainProcess(View view) {
final String TAG="DEBUG";
Log.d(TAG,"At least process started");
EditText owner = (EditText) findViewById(R.id.name);
owner_string = owner.getText().toString();
Log.d(TAG,"owner name stored");
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
setContentView(R.layout.activity_main_screen);
Log.d(TAG,"content shown");
}
}
activity_main_screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/what_would_you_like_me_to_do_today"
android:textSize="18sp" />
<Button
android:id="@+id/Cam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="@string/camera" />
<Button
android:id="@+id/Mus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="@string/music" />
<Button
android:id="@+id/QR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="@string/scanQR" />
<EditText
android:id="@+id/toastText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/order"
android:inputType="textPersonName" />
<Button
android:id="@+id/toaster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:onClick="toaster"
android:padding="0dp"
android:paddingTop="15dp"
android:text="@string/toast"
android:layout_marginEnd="100dp"
android:layout_gravity="center"/>
</LinearLayout>
MainScreen.java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
}
public void toaster(View view){
EditText toast = (EditText)findViewById(R.id.toastText);
String final_toast = toast.getText().toString();
Toast.makeText(getApplicationContext(), final_toast, Toast.LENGTH_SHORT).show();
}
}
編輯:至於建議,我感動toaser功能FirstActivity.Java並刪除了MainScreen.java文件,因爲它變得毫無意義,保持。但主要問題是當我按下按鈕(id命名)應用程序不斷停止。
EDIT2:我發現的setContentView(R.layout.activity_main_screen)在FirstActivity.Java需要高於此代碼
TextView textView = (TextView) findViewById(R.id.welcome); textView.setText("Hi " + owner_string + "."); Log.d(TAG,"owner name is set");
使得活動有權訪問所有的佈局組件。 感謝解決:)
的代碼是完全錯誤的,這是一個錯誤的方式來改變你的活動通過這樣的內容,你MainScreen活動將留在黑暗和永遠不會被使用。 – Maysara