我正在尝试制作多个打开不同XML页面的按钮。我在before.java之前得到了这个,并且由于某种原因我得到错误“没有封闭的MainActivity类型的实例可以在范围内访问”。继承我的代码:
package com.dogger20011.app;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public abstract class beforemain extends MainActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button mBtn1 = (Button) findViewById(R.id.button1);
Button mBtn2 = (Button) findViewById(R.id.button2); //Just like #1
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this); //Also like #1
}
@Override
public void onClick(View v) {
switch(v.getId()) { //Get the id of the button that was clicked
case R.id.button1:
Intent i = new Intent(MainActivity.this, Activity2.class);
startActivity(i);
break;
case R.id.button2:
Intent i1 = new Intent(MainActivity.this, Activity3.class);
startActivity(i1);
break;
}
}}
这里是按钮1和2
的错误case R.id.button2:
Intent i1 = new Intent(MainActivity.this, Activity3.class);
我做错了什么?
修改 继承我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dogger20011.app"
android:versionCode="1"
android:versionName="1.0.5Beta1"
android:installLocation="auto" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission
android:name="android.permission.INTERNET"
android:maxSdkVersion="19">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:label="@string/app_name"
android:name="beforemain">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".Activity2"></activity>
<activity
android:label="@string/app_name"
android:name=".Activity3"></activity>
</application>
</manifest>
编辑2 继承我的错误日志:
06-16 01:36:55.379: E/AndroidRuntime(1066): FATAL EXCEPTION: main
06-16 01:36:55.379: E/AndroidRuntime(1066): Process: com.dogger20011.app, PID: 1066
06-16 01:36:55.379: E/AndroidRuntime(1066): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dogger20011.app/com.dogger20011.app.beforemain}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.dogger20011.app:id/container) for fragment PlaceholderFragment{b3d37f00 #0 id=0x7f05003c}
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.os.Looper.loop(Looper.java:136)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 01:36:55.379: E/AndroidRuntime(1066): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 01:36:55.379: E/AndroidRuntime(1066): at java.lang.reflect.Method.invoke(Method.java:515)
06-16 01:36:55.379: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 01:36:55.379: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 01:36:55.379: E/AndroidRuntime(1066): at dalvik.system.NativeStart.main(Native Method)
06-16 01:36:55.379: E/AndroidRuntime(1066): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.dogger20011.app:id/container) for fragment PlaceholderFragment{b3d37f00 #0 id=0x7f05003c}
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.app.Activity.performStart(Activity.java:5241)
06-16 01:36:55.379: E/AndroidRuntime(1066): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
06-16 01:36:55.379: E/AndroidRuntime(1066): ... 11 more
编辑3 这是我的MainActivity,它包含占位符片段:
package com.dogger20011.app;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new
PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.Downloads) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
private static final Context context = null;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
答案 0 :(得分:0)
更改此
new Intent(MainActivity.this, Activity3.class);
到
new Intent(beforemain.this, Activity3.class);
这是必要的,因为您创建的用作监听器的匿名类是beforemain
类的内部,而不是MainActivity
类。
答案 1 :(得分:0)
我在R.id.container
xml中找不到ID activity_main
,您可以在布局中添加ID,也可以删除R.id.container
方法中的add
更改此
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new
。PlaceholderFragment())提交(); }
以强>
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(new
PlaceholderFragment(), "placeholder").commit();
}