2015-05-15 4 views
3

У меня есть простое приложение с одной кнопкой на каждом из трех экранов.
Первые две кнопки должны отправить вас от одного намерения до следующего, пока последняя кнопка не примет фотографию и не напечатает ее на третьем экране.
Но когда я добавил функцию камеры, приложение перестало работать, и теперь оно даже не переместится во второе намерение, когда я нажимаю первую кнопку «Начать спал».java.lang.IllegalStateException: Не удалось найти метод (Показать)

HomeActivity

package com.example.daniel.proto; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 


public class Home extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
    } 


    public void onClick(View view) 
    { 
     Intent i = new Intent(this, SelectLevel.class); 
     startActivity(i); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_home, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

SelectLevelActivity

package com.example.daniel.proto; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.content.Intent; 


public class SelectLevel extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_select_level); 
    } 

    public void onClick(View view) 
    { 
     Intent ii = new Intent (this, QRscanner.class); 
     startActivity(ii); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_select_level, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

QRscannerActivity

package com.example.daniel.proto; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.graphics.Bitmap; 
import android.provider.MediaStore; 
import android.content.pm.PackageInfo; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 


public class QRscanner extends ActionBarActivity 
{ 

    static final int REQUEST_IMAGE_CAPTURE = 1; 
    ImageView photoImageView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_qrscanner); 

     Button photoButton = (Button) findViewById(R.id.photoButton); 
     photoImageView = (ImageView) findViewById(R.id.photoImageView); 

     //Disable the button if the user doesn't have a camera 
     if(!hasCamera()) 
      photoButton.setEnabled(false); 


    } 

    //Check if the user has a camera 
    private boolean hasCamera() 
    { 
     return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY); 
    } 

    //Launching the camera 
    public void launchCamera(View view) 
    { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     //Take a picture and pass results along to onActivityResult 
     startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); 

    } 

    //If you want to return the image taken 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) 
     { 
      //Get the photo 
      Bundle extras = data.getExtras(); 
      Bitmap photo = (Bitmap) extras.get("data"); 
      photoImageView.setImageBitmap(photo); 
     } 
    } 
} 

Главная XML

<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" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Home" 
    android:background="#009900"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Start Spil" 
     android:id="@+id/playButton" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:onClick="" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="QuesteX" 
     android:id="@+id/textView" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="36dp" 
     android:textSize="40dp" /> 

</RelativeLayout> 

SelectLevel XML

<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" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.daniel.proto.SelectLevel" 
    android:background="#72231F"> 

    TextView android:text="@string/hello_world" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Udfordringer" 
     android:id="@+id/titleText" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textColor="#FFF" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/homeButton" 
     android:onClick="onClick" 
     android:layout_below="@+id/titleText" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="55dp" 
     android:layout_marginStart="55dp" 
     android:layout_marginTop="49dp" 
     android:text="Click me" /> 
</RelativeLayout> 

QRscanner XML

<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" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.daniel.proto.QRscanner" 
    android:background="#990099"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Take Photo" 
     android:id="@+id/photoButton" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="65dp" 
     android:onClick="launchCamera" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/photoImageView" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="50dp" 
     android:minHeight="300dp" 
     android:minWidth="300dp" /> 
</RelativeLayout> 

LogCat

05-15 19:41:01.902 2927-2927/com.example.daniel.proto E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.daniel.proto, PID: 2927 
    java.lang.IllegalStateException: Could not find a method (View) in the activity class com.example.daniel.proto.Home for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'playButton' 
      at android.view.View$1.onClick(View.java:4007) 
      at android.view.View.performClick(View.java:4780) 
      at android.view.View$PerformClick.run(View.java:19866) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5257) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: java.lang.NoSuchMethodException: [class android.view.View] 
      at java.lang.Class.getMethod(Class.java:664) 
      at java.lang.Class.getMethod(Class.java:643) 
      at android.view.View$1.onClick(View.java:4000) 
            at android.view.View.performClick(View.java:4780) 
            at android.view.View$PerformClick.run(View.java:19866) 
            at android.os.Handler.handleCallback(Handler.java:739) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5257) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
+0

Вы забыли прикрепить иконки на ваш «вопрос». Или вы забыли сами попытаться решить проблему самостоятельно –

ответ

5

вы случайно удалили метод внутри андроида: OnClick кнопки.

изменение:

android:onClick="" 

к:

android:onClick="onClick" 
0

В вашей первой деятельности, у вас есть метод, называемый OnClick (View вид). Он будет вызываться нажатием кнопки Button, называемой playButton. Но в вашем файле макета xml вы должны указать имя метода в вашем теге onClick. В противном случае вы должны написать слушателя в вашей деятельности то, как показано ниже:

button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on click 
     } 
    }); 

и удалить этот тег:

android:onClick="" 
Смежные вопросы