2017-07-04 62 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

相关问题