我有两个活动:MainActivity和MenuActivity。如果我尝试在SplashActivity之后启动MainActivity,它可以正常工作,但是如果我尝试使用MenuActivity做同样的事情,它会给我一个错误,我做错了什么?。
清单:
<activity
android:name=".SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MenuActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MENUACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
SplashActivity.java:
public class SplashActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Thread timer = new Thread(){
public void run (){
try {
Thread.sleep(5000);
} catch(Exception e){
} finally {
Intent openMenuActivity = new Intent("android.intent.action.MAINACTIVITY");
//THIS WORKS FINE
//HOWEVER,
//Intent openMenuActivity = new Intent("android.intent.action.MENUACTIVITY");
//THIS DOESN'T, WHY?
startActivity(openMenuActivity);
}
}
};
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_splashactivity);
timer.start();
}
}
MenuActivity.java:
package com.test.test1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_menuactivity);
}
}
MenuButtonView.java:
package com.test.test1;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class MenuButtonView extends View{
public MenuButtonView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MenuButtonView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MenuButtonView(Context context, AttributeSet attrs, int in) {
super(context, attrs, in);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
}
}
layout_menuactivity.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" >
<com.test.test1.MenuButtonView
android:id="@+id/menubuttonview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".7"
android:layout_gravity="bottom" />
<com.test.test1.MenuLogoView
android:id="@+id/menulogoview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".3"
android:layout_gravity="top" />
</LinearLayout>
LOGCAT:
08-22 04:01:21.649: E/AndroidRuntime(274): FATAL EXCEPTION: main
08-22 04:01:21.649: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test1/com.test.test1.MenuActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class com.test.test1.MenuLogoView
08-22 04:01:21.649: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-22 04:01:21.649: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 04:01:21.649: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
08-22 04:01:21.649: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-22 04:01:21.649: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-22 04:01:21.649: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
08-22 04:01:21.649: E/AndroidRuntime(274): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.test.test1.MenuLogoView
08-22 04:01:21.649: E/AndroidRuntime(274): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-22 04:01:21.649: E/AndroidRuntime(274): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.app.Activity.setContentView(Activity.java:1647)
08-22 04:01:21.649: E/AndroidRuntime(274): at com.test.test1.MenuActivity.onCreate(MenuActivity.java:12)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-22 04:01:21.649: E/AndroidRuntime(274): ... 11 more
08-22 04:01:21.649: E/AndroidRuntime(274): Caused by: java.lang.NoSuchMethodException: MenuLogoView(Context,AttributeSet)
08-22 04:01:21.649: E/AndroidRuntime(274): at java.lang.Class.getMatchingConstructor(Class.java:660)
08-22 04:01:21.649: E/AndroidRuntime(274): at java.lang.Class.getConstructor(Class.java:477)
08-22 04:01:21.649: E/AndroidRuntime(274): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
08-22 04:01:21.649: E/AndroidRuntime(274): ... 21 more
答案 0 :(得分:0)
该错误表示您需要为MenuButtonView
创建构造函数, xml格式中使用的自定义视图必须实现三个默认构造函数:
确保您的MenuButtonView
类
MenuButtonView(Context context,AttributeSet attrs)
MenuButtonView(Context context)
MenuButtonView(Context context, AttributeSet attrs, int defStyleAttr)
答案 1 :(得分:0)
错误在layout_menuactivity.xml文件中。您正在参考MenuActivity类的某些方法,该方法尚未在该类中实现。它的投掷错误。检查一下布局文件。
答案 2 :(得分:0)
检查R.layout.layout_menuactivity
布局。我认为存在问题。
您使用了自定义视图名称MenuLogoView
而没有结构。
创建如下的结构:
MenuButtonView(Context context,AttributeSet attr)