在Android工作室测试了一些东西并遇到了这个错误:
致命的例外:主要 过程:com.meisolsson.app,PID:25610 java.lang.RuntimeException:无法启动活动ComponentInfo {com.meisolsson.app/com.meisolsson.app.LoginActivity}:java.lang.IllegalArgumentException:找不到id 0x7f070053的视图(com.meisolsson.app:id/container)对于片段PlaceholderFragment {4284e750#0 id = 0x7f070053}
这是我的LoginActivity.java
package com.meisolsson.app;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
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;
import android.view.Window;
import android.widget.Button;
public class LoginActivity extends ActionBarActivity {
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
this.setContentView(R.layout.fragment_login);
this.loginButton = (Button)this.findViewById(R.id.button);
this.loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
return rootView;
}
}
}
我的fragment_login.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=".LoginActivity$PlaceholderFragment"
android:focusable="true"
android:background="#fcff00">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="@string/user"
android:padding="10dp"
android:background="#ffffff"
android:layout_above="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="@string/pass"
android:background="#ffffff"
android:padding="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
android:id="@+id/button"
style="@style/AppTheme"
android:background="#ff5900"
android:shadowColor="#000000"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText"
android:layout_alignRight="@+id/editText2"
android:clickable="true"/>
</RelativeLayout>
答案 0 :(得分:1)
问题在于你这样做的方式。对于Activity
,您没有设置Fragment
的视图,您设置了一个单独的视图,该视图有一个放置Fragment
的位置。根据您PlaceHolderFragment
的执行方式,以下内容应该是一个有效的应用程序。
首先添加此文件:
activity_main.xml中
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
然后在Activity
:
LoginActivity.java
/* Same stuff*/ @Override protected void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); //This is the important change right here! setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); } //The stuff that you had handling buttons should go in your //PlaceHolderFragment class's onCreateView() method }
我希望这会有所帮助。祝你好运!