java.lang.IllegalStateException:无法找到一个方法(视图) [英] java.lang.IllegalStateException: Could not find a method (View)
问题描述
我有每三个屏幕中的一个按钮,一个简单的应用程序。结果
前两个按钮应该从一个意图送你到下一个,直到最后一个按钮应该拍一张照片,并打印在第三个屏幕上。结果
但是,当我加了摄像头功能的应用程序停止工作,现在它甚至不会移动到第二个意图时,我preSS的第一个矽品开始按钮。
HomeActivity
包com.example.daniel.proto;进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v7.app.ActionBarActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
公共类家庭扩展ActionBarActivity { @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_home);
}
公共无效的onClick(查看视图)
{
意图I =新意图(这一点,SelectLevel.class);
startActivity(ⅰ);
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.menu_home,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId(); // noinspection SimplifiableIfStatement
如果(ID == R.id.action_settings){
返回true;
} 返回super.onOptionsItemSelected(项目);
}
}
SelectLevelActivity
包com.example.daniel.proto;进口android.support.v7.app.ActionBarActivity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.content.Intent;
公共类SelectLevel扩展ActionBarActivity { @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_select_level);
} 公共无效的onClick(查看视图)
{
意图II =新意图(这一点,QRscanner.class);
startActivity(二);
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.menu_select_level,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId(); // noinspection SimplifiableIfStatement
如果(ID == R.id.action_settings){
返回true;
} 返回super.onOptionsItemSelected(项目);
}
}
QRscannerActivity
包com.example.daniel.proto;进口android.support.v7.app.ActionBarActivity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.content.Intent;
进口android.content.pm.PackageManager;
进口android.graphics.Bitmap;
进口android.provider.MediaStore;
进口android.content.pm.PackageInfo;
进口android.view.View;
进口android.widget.Button;
进口android.widget.ImageView;
公共类QRscanner扩展ActionBarActivity
{ 静态最终诠释REQUEST_IMAGE_CAPTURE = 1;
ImageView的photoImageView;
@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_qrscanner); 按钮photoButton =(按钮)findViewById(R.id.photoButton);
photoImageView =(ImageView的)findViewById(R.id.photoImageView); //禁用按钮,如果用户不有一个摄像头
如果(!hasCamera())
photoButton.setEnabled(假);
} //检查用户是否有一个照相机
私人布尔hasCamera()
{
返回getPackageManager()hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)。
} //启动摄像头
公共无效launchCamera(查看视图)
{
意向意图=新意图(MediaStore.ACTION_IMAGE_CAPTURE);
//拍摄一张照片,并一起传递的onActivityResult结果
startActivityForResult(意向,REQUEST_IMAGE_CAPTURE); } //如果你想返回拍摄的图像
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
如果(要求code == REQUEST_IMAGE_CAPTURE&放大器;&安培;结果code == RESULT_OK)
{
//获取照片
捆绑额外= data.getExtras();
位图照片=(位图)extras.get(数据);
photoImageView.setImageBitmap(照片);
}
}
}
首页XML
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools的android:layout_width =match_parent
机器人:layout_height =match_parent机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
机器人:paddingBottom会=@扪/ activity_vertical_margin工具:主页上下文=
机器人:背景=#009900> <按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=开始矽品
机器人:ID =@ + ID /为playButton
机器人:layout_centerVertical =真
机器人:layout_centerHorizontal =真
安卓的onClick =
/> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceLarge
机器人:文字=QuesteX
机器人:ID =@ + ID / TextView的
机器人:layout_alignParentTop =真
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =36dp
机器人:TEXTSIZE =40dp/>< / RelativeLayout的>
SelectLevel XML
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools的android:layout_width =match_parent
机器人:layout_height =match_parent机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
机器人:paddingBottom会=@扪/ activity_vertical_margin
工具:上下文=com.example.daniel.proto.SelectLevel
机器人:背景=#72231F> TextView的机器人:文本=@字符串/参考hello world的android:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceLarge
机器人:文字=Udfordringer
机器人:ID =@ + ID / titleText
机器人:layout_alignParentTop =真
机器人:layout_centerHorizontal =真
机器人:文字颜色=#FFF/> <按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID /为homeButton
安卓的onClick =的onClick
机器人:layout_below =@ + ID / titleText
机器人:layout_alignParentLeft =真
机器人:layout_alignParentStart =真
机器人:layout_marginLeft =55dp
机器人:layout_marginStart =55dp
机器人:layout_marginTop =49dp
机器人:文字=点击我/>
< / RelativeLayout的>
QRscanner XML
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools的android:layout_width =match_parent
机器人:layout_height =match_parent机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
机器人:paddingBottom会=@扪/ activity_vertical_margin
工具:上下文=com.example.daniel.proto.QRscanner
机器人:背景=#990099> <按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=拍摄照片
机器人:ID =@ + ID / photoButton
机器人:layout_alignParentBottom =真
机器人:layout_centerHorizontal =真
机器人:layout_marginBottom =65dp
安卓的onClick =launchCamera/> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / photoImageView
机器人:layout_alignParentTop =真
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =50dp
安卓了minHeight =300dp
安卓了minWidth =300dp/>
< / RelativeLayout的>
LogCat中
05-15 19:41:01.902 2927年至2927年/ com.example.daniel.proto E / AndroidRuntime:致命异常:主要
工艺:com.example.daniel.proto,PID:2927
java.lang.IllegalStateException:找不到在活动课com.example.daniel.proto.Home为的onClick处理方法(视图)的视图类android.support.v7.widget.AppCompatButton id为为playButton
在android.view.View $ 1.onClick(View.java:4007)
在android.view.View.performClick(View.java:4780)
在android.view.View $ PerformClick.run(View.java:19866)
在android.os.Handler.handleCallback(Handler.java:739)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5257)
在java.lang.reflect.Method.invoke(本机方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
java.lang.NoSuchMethodException:所致[类android.view.View]
在java.lang.Class.getMethod(Class.java:664)
在java.lang.Class.getMethod(Class.java:643)
在android.view.View $ 1.onClick(View.java:4000)
在android.view.View.performClick(View.java:4780)
在android.view.View $ PerformClick.run(View.java:19866)
在android.os.Handler.handleCallback(Handler.java:739)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5257)
在java.lang.reflect.Method.invoke(本机方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
在意外删除从Android里面的方法:的onClick按钮
变化:
安卓的onClick =
到
安卓的onClick =的onClick
I have a simple app with one button on each of the three screens.
The first two buttons should send you from one intent to the next until the last button which should take a photo and print it on the third screen.
But when I added the camera function the app stopped working and now it won't even move to the second intent when I press the first "Start spil" button.
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);
}
}
}
Home 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)
you accidentally removed the method from inside the android:onClick of the button.
change:
android:onClick=""
to:
android:onClick="onClick"
这篇关于java.lang.IllegalStateException:无法找到一个方法(视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!