2017-07-04 60 views
-1

我正在製作一個簡單的應用程序,其中上下文視圖發生更改,然後顯示作爲用戶輸入的輸入信息。我不明白爲什麼應用程序在更改上下文視圖時不斷崩潰。方法丟失或簽名不正確

而且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"); 

使得活動有權訪問所有的佈局組件。 感謝解決:)

+0

的代碼是完全錯誤的,這是一個錯誤的方式來改變你的活動通過這樣的內容,你MainScreen活動將留在黑暗和永遠不會被使用。 – Maysara

回答

1

您有佈局包括onClick屬性替換的內容視圖中的第一個活動,但你有沒有public void toaster(View view)方法那裏。

因此,要麼第二次不要使用setContentView,要麼在這兩個活動上實施該方法。

推薦的方法,以取代觀點是碎片,順便

+0

如果要永久替換整個佈局,設置新的內容視圖是完全可以接受的替代方法。如果您想在多個地方重複使用相同的控制邏輯,或者想將其作爲UI元素嵌入到另一個xml文件中,則應使用碎片。他在這裏使用Fragments並不會錯,但它幾乎沒有要求(並且片段替換有這麼多問題,所以我毫不猶豫地建議最近在任何地方使用它)。雖然看他的代碼實際上在做什麼,但我不確定他真正想要什麼。 –

+0

調用setContentView並期待視圖引用保持其問題,所以我會說這是公平的交易 –

+0

哦,如果你設置了一個新的上下文視圖,所有的引用都需要被清除。但是這種技術並不差,儘管他的實施並不好。 –

0

這是一種錯誤的方式來改變你的活動內容,這裏的主要問題是,你必須onclick屬性設置爲「烤麪包機」並且您的FirstActivity中沒有稱爲「烤麪包機」的功能。

除此之外,您的代碼中的MainScreen Activity永遠不會被使用。

所以,你的問題是你將FirstActivity的內容設置爲「activity_main_screen」。xml「,並且FirstActivity在其中沒有」toaster「方法,當您更改上下文時,Activity將嘗試找到Toaster方法,並且該應用會因爲FirstActivity中不存在該方法而崩潰

You可以通過進行所謂的「烤麪包機」裏面FirstActivity方法解決這個問題,但 這是一個非常不好的做法,您可以使用片段代替,或者你可以使用意向轉移到另一個活動。

約碎片一些有用的鏈接:

https://developer.android.com/guide/components/fragments.html

https://www.tutorialspoint.com/android/android_fragments.htm

https://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/

Why should I use fragment in Android?

+0

我是android新手,對片段不太瞭解。我會嘗試將它用於我的代碼,但我的代碼可能出錯的地方。任何幫助 – Nautatva

+0

我剛剛編輯我的答案,看看。 – Maysara

相關問題