我已经在这里阅读了很多关于这个错误的页面片段,但到目前为止没有任何帮助。我得到的错误是:
android.view.InflateException: Binary XML file line #10: error inflating class fragment
我正在制作一个Twitter客户端应用,并使用 ComposeTweetActivity.java 中的片段。所以我有这个类和一个片段类,然后是每个的布局。我将发布所有这4个文件和我的LogCat。如果您需要了解更多信息,请与我们联系。非常感谢你提前。
CompostTweetActivity.java
package com.codepath.apps.mytwitterapp.activities;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import com.codepath.apps.mytwitterapp.R;
import com.codepath.apps.mytwitterapp.fragments.ComposeTweetFragment.OnComposeTweetListener;
import com.codepath.apps.mytwitterapp.models.Tweet;
/**
* Class hosts fragment for composing and posting tweets
*/
public class ComposeTweetActivity extends ActionBarActivity implements OnComposeTweetListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compose_tweet);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.compose_tweet, menu);
return true;
}
@Override
public void onTweetCanceled() {
Intent i = new Intent();
setResult(RESULT_CANCELED, i);
finish();
}
@Override
public void onTweetPosted(Tweet postedTweet) {
Intent i = new Intent();
i.putExtra("new_tweet", postedTweet);
setResult(RESULT_OK, i);
finish();
}
}
ComposeTweetFragment.java
package com.codepath.apps.mytwitterapp.fragments;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.codepath.apps.mytwitterapp.R;
import com.codepath.apps.mytwitterapp.activities.MyTwitterApp;
import com.codepath.apps.mytwitterapp.helpers.AsyncTweetSave;
import com.codepath.apps.mytwitterapp.models.Tweet;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;
/**
* Class represents a screen to compose and post tweets. User is warned if text
* typed passes the 140-character limit
*/
public class ComposeTweetFragment extends Fragment {
private Activity activity;
private Button btnCancel,
btnTweet;
private ImageView ivUserImage;
private TextView tvUserName;
private EditText etNewTweet;
private boolean alreadyToasted = false;
private OnComposeTweetListener listener;
public interface OnComposeTweetListener {
public void onTweetPosted(Tweet postedTweet);
public void onTweetCanceled();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnComposeTweetListener) {
listener = (OnComposeTweetListener) activity;
} else {
throw new ClassCastException(activity.toString() + " must implement "
+ "OnTweetComposedListener interface");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
super.onCreateView(inflater, parent, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_compose_tweet, parent, false);
setupButtons(v);
setupImageView(v);
setupTextView(v);
setupEditText(v);
return v;
}
private void setupButtons(View v) {
btnCancel = (Button) v.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onTweetCanceled();
}
});
btnTweet = (Button) v.findViewById(R.id.btnTweet);
btnTweet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tweetBody = etNewTweet.getText().toString();
tweet(tweetBody);
}
});
}
private void setupImageView(View v) {
ivUserImage = (ImageView) v.findViewById(R.id.ivUserImage);
ImageLoader.getInstance().displayImage(getActivity().getIntent()
.getStringExtra("user_image_url"), ivUserImage);
}
private void setupTextView(View v) {
tvUserName = (TextView) v.findViewById(R.id.tvUserName);
tvUserName.setText("@" + getActivity().getIntent().getStringExtra("screen_name"));
}
private void setupEditText(View v) {
etNewTweet = (EditText) v.findViewById(R.id.etNewTweet);
// Show soft keyboard when EditText field requests focus
if (etNewTweet.requestFocus()) {
InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(etNewTweet, InputMethodManager.SHOW_IMPLICIT);
}
etNewTweet.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!alreadyToasted && s.length() == 140) {
Toast.makeText(activity, "You've reached the 140-character"
+ " limit", Toast.LENGTH_LONG).show();
alreadyToasted = true;
}
else if (s.length() > 140) {
etNewTweet.setTextColor(Color.RED);
} else {
etNewTweet.setTextColor(Color.BLACK);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable s) { }
});
}
private void tweet(String tweetBody) {
MyTwitterApp.getRestClient().postTweet(tweetBody, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, JSONObject jsonTweetResponse) {
Tweet newTweet = Tweet.fromJson(jsonTweetResponse);
new AsyncTweetSave().execute(newTweet); // Might need to disable
listener.onTweetPosted(newTweet);
}
@Override
public void onFailure(Throwable e, JSONObject error) {
Log.e("ERROR", e.getMessage());
}
});
}
}
activity_compose_tweet.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#185277"
tools:context="com.codepath.apps.mytwitterapp.activities.ComposeTweetActivity"
tools:ignore="MergeRootFrame" >
<fragment
android:id="@+id/fragmentComposeTweet"
android:name="com.codepath.apps.mytwitterapp.fragments.ComposeTweetFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
tools:layout="@android:layout/fragment_compose_tweet" />
</RelativeLayout>
fragment_compose_tweet.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#185277" >
<Button
android:id="@+id/btnCancel"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:background="@drawable/cancel_button_outline"
android:textColor="#ffffff"
android:text="@string/btn_cancel" />
<Button
android:id="@+id/btnTweet"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:background="@drawable/tweet_button_outline"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="@string/btn_tweet" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignLeft="@+id/btnCancel"
android:layout_below="@+id/btnCancel"
android:layout_marginRight="20dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="#ffffff"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ivUserImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="@drawable/ic_action_user_profile_dark" />
<TextView
android:id="@+id/tvUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/ivUserImage"
android:textStyle="bold|italic"
android:text="@string/twitter_handle" />
</RelativeLayout>
<EditText
android:id="@+id/etNewTweet"
android:layout_width="match_parent"
android:layout_height="160dp"
android:padding="10dp"
android:background="#ffffff"
android:gravity="top"
android:hint="@string/et_new_tweet_hint"
android:inputType="textEmailAddress|textCapSentences|textMultiLine"
android:imeOptions="actionDone" />
</LinearLayout>
</RelativeLayout>
logcat的
04-19 06:20:15.443: E/AndroidRuntime(8555): FATAL EXCEPTION: main
04-19 06:20:15.443: E/AndroidRuntime(8555): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.codepath.apps.mytwitterapp/com.codepath.apps.mytwitterapp.activities.ComposeTweetActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.os.Looper.loop(Looper.java:137)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-19 06:20:15.443: E/AndroidRuntime(8555): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 06:20:15.443: E/AndroidRuntime(8555): at java.lang.reflect.Method.invoke(Method.java:511)
04-19 06:20:15.443: E/AndroidRuntime(8555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-19 06:20:15.443: E/AndroidRuntime(8555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-19 06:20:15.443: E/AndroidRuntime(8555): at dalvik.system.NativeStart.main(Native Method)
04-19 06:20:15.443: E/AndroidRuntime(8555): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-19 06:20:15.443: E/AndroidRuntime(8555): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.app.Activity.setContentView(Activity.java:1867)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
04-19 06:20:15.443: E/AndroidRuntime(8555): at com.codepath.apps.mytwitterapp.activities.ComposeTweetActivity.onCreate(ComposeTweetActivity.java:19)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.app.Activity.performCreate(Activity.java:5008)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-19 06:20:15.443: E/AndroidRuntime(8555): ... 11 more
04-19 06:20:15.443: E/AndroidRuntime(8555): Caused by: java.lang.NullPointerException
04-19 06:20:15.443: E/AndroidRuntime(8555): at com.codepath.apps.mytwitterapp.fragments.ComposeTweetFragment.setupEditText(ComposeTweetFragment.java:113)
04-19 06:20:15.443: E/AndroidRuntime(8555): at com.codepath.apps.mytwitterapp.fragments.ComposeTweetFragment.onCreateView(ComposeTweetFragment.java:74)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
04-19 06:20:15.443: E/AndroidRuntime(8555): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
答案 0 :(得分:2)
更改此
InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
到
InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
此
private Activity activity;
未在任何地方初始化