fragment
我的应用崩溃时打开MainActivity.class
。
MainActivity.class
/* Menu */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_server:
Intent intent = new Intent(getApplicationContext(), AddServerFragment.class);
startActivity(intent);
return true;
case R.id.menu_refresh:
myWebView.reload();
return true;
default:
return true;
}
...
public class AddServerFragment extends Fragment {
public AddServerFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.add_ip, container, false);
Button btn_back, btn_add;
final EditText server_ip, server_name;
server_ip = (EditText) findViewById(R.id.edit_server_address);
server_name = (EditText) findViewById(R.id.edit_server_name);
/* Back Button */
btn_back = (Button) findViewById(R.id.btn_back);
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
/* Add IP Button */
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String new_server_ip = null, new_server_name = null;
ArrayList<String> server_name_list = new ArrayList<String>();
ArrayList<String> server_ip_list = new ArrayList<String>();
new_server_ip = server_ip.getText().toString();
server_ip_list.add(new_server_ip);
new_server_name = server_name.getText().toString();
server_name_list.add(new_server_name);
}
});
return rootView;
}
}
我的错误日志只说,我必须在我的Manifest.xml中添加Fragment作为活动。但我知道,我不必在那里添加它。
错误日志
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: FATAL EXCEPTION: main
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: Process: de.kwietzorek.fulcrumwebview, PID: 18345
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.kwietzorek.fulcrumwebview/de.kwietzorek.fulcrumwebview.MainActivity$AddServerFragment}; have you declared this activity in your AndroidManifest.xml?
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3917)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3877)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:4200)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:4168)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at de.kwietzorek.fulcrumwebview.MainActivity.onOptionsItemSelected(MainActivity.java:90)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.app.Activity.onMenuItemSelected(Activity.java:2908)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:621)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.widget.AdapterView.performItemClick(AdapterView.java:310)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.widget.AbsListView$3.run(AbsListView.java:3879)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-14 15:22:05.935 18345-18345/de.kwietzorek.fulcrumwebview E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
修改
我的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:2)
Wrong with the above code:
ERROR Intent intent = new Intent(getApplicationContext(),AddServerFragment.class); startActivity(意向);
Use the correct method for using fragment, you have used method for starting activity.
AddServerFragment是Fragment not activity。
要在活动中添加片段,您必须使用以下方法:
在您的活动中,
activity_main.xml中的XML代码部分:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity.class中的Java代码部分:
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create an instance of AddServerFragment
AddServerFragment firstFragment = new AddServerFragment();
// if there are any extras
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
答案 1 :(得分:0)
您需要将活动声明添加到清单文件中:
<activity android:name=".MainActivity" >
</activity>
答案 2 :(得分:0)
当您想要打开片段时,您无法使用意图。 您可以尝试FragmentManager。
示例:
public void getFragment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
Fragment mFragment = fm.findFragmentById(R.id.fragment_container);
if (mFragment == null) {
mFragment = fragment;
fm.beginTransaction()
.add(R.id.fragment_container, mFragment)
.commit();
}
if (mFragment != null) {
mFragment = fragment;
fm.beginTransaction().addToBackStack(null)
.replace(R.id.fragment_container, mFragment)
.commit();
}
}