0

我正在为平板电脑和手机开发一个安卓应用程序。我编写了自己的片段类及其相应的活动。但是当我运行应用程序时,我收到以下错误。

LogCat

07-23 18:56:10.200: E/AndroidRuntime(691): FATAL EXCEPTION: main
07-23 18:56:10.200: E/AndroidRuntime(691): java.lang.RuntimeException: Unable to start activity        ComponentInfo{com.fxpal.unity.android/com.fxpal.unity.android.MainActivity}: android.view.InflateException:   Binary XML file line #7: Error inflating class fragment
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.os.Looper.loop(Looper.java:137)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.ActivityThread.main(ActivityThread.java:4424)
07-23 18:56:10.200: E/AndroidRuntime(691):  at java.lang.reflect.Method.invokeNative(Native Method)
07-23 18:56:10.200: E/AndroidRuntime(691):  at java.lang.reflect.Method.invoke(Method.java:511)
07-23 18:56:10.200: E/AndroidRuntime(691):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-23 18:56:10.200: E/AndroidRuntime(691):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-23 18:56:10.200: E/AndroidRuntime(691):  at dalvik.system.NativeStart.main(Native Method)
07-23 18:56:10.200: E/AndroidRuntime(691): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-23 18:56:10.200: E/AndroidRuntime(691):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.Activity.setContentView(Activity.java:1835)
07-23 18:56:10.200: E/AndroidRuntime(691):  at com.fxpal.unity.android.MainActivity.onCreate(MainActivity.java:20)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.Activity.performCreate(Activity.java:4465)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-23 18:56:10.200: E/AndroidRuntime(691):  ... 11 more
07-23 18:56:10.200: E/AndroidRuntime(691): Caused by: java.lang.NullPointerException
07-23 18:56:10.200: E/AndroidRuntime(691):  at com.fxpal.unity.android.EveryoneAdapter.<init>(EveryoneAdapter.java:24)
07-23 18:56:10.200: E/AndroidRuntime(691):  at com.fxpal.unity.android.GridFragment.onCreateView(GridFragment.java:21)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:806)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1010)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1108)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.app.Activity.onCreateView(Activity.java:4243)
07-23 18:56:10.200: E/AndroidRuntime(691):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
07-23 18:56:10.200: E/AndroidRuntime(691):  ... 21 more

这是我的 EveryoneAdapter 类

   package com.fxpal.unity.android;

  import java.util.ArrayList;

  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.GridView;
  import android.widget.ImageView;


 public class EveryoneAdapter extends BaseAdapter {
private Context mContext;
private UnityMobileApp appCtx;
private ArrayList<String> mNamesList;

public EveryoneAdapter(Context c, UnityMobileApp a) {
    mContext = c;
    appCtx = a;
    mNamesList = new ArrayList<String>(a.everyone.keySet());
}

public int getCount() {
    System.out.println(mNamesList.size());
    return mNamesList.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    Person p = appCtx.everyone.get(mNamesList.get(position));

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);

        imageView.setLayoutParams(new GridView.LayoutParams(66, 66));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);

    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageBitmap(p.getRoundedImage(Consts.SMALL_USER_IMAGE_SIZE, Consts.SMALL_USER_IMAGE_BORDER)); 
    imageView.setContentDescription(p.getUsername());
  // example adapter  imageView.setImageResource(mThumbIds[position]);
    final String mUsername = p.getUsername();
    imageView.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            Intent i = new Intent();
            String pkg_name = EveryoneAdapter.class.getPackage().getName();
            i.setComponent(new ComponentName(pkg_name, pkg_name + ".IndividualViewActivity"));
            i.putExtra(Consts.EXTRA_USERNAME, mUsername);
            mContext.startActivity(i);
        }
    });

    return imageView;
}
}

我的 GridFragment 课程是

 package com.fxpal.unity.android;

 import com.fxpal.unity.android.EveryoneAdapter;
 import android.app.Fragment;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.GridView;


 public class GridFragment extends Fragment{
public UnityMobileApp appCtx;
public EveryoneAdapter everyoneAdapter;
@Override
 public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    {
    View view = inflater.inflate(R.layout.gridview,container,false);
   if(everyoneAdapter == null || everyoneAdapter.getCount()!=appCtx.everyone.size()){
    GridView gridView = (GridView) view.findViewById(R.id.gridView);
    gridView.setAdapter(new EveryoneAdapter( view.getContext(),appCtx));
    }// uses the view to get the context instead of getActivity().
    else
    {
        everyoneAdapter.notifyDataSetChanged();
    }
    return view;
}
 public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);
 }

}

我的 IndividualViewFragment 类在下面

 package com.fxpal.unity.android;

 import com.fxpal.unity.android.Person;
 import android.app.Dialog;
 import android.app.Fragment;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
 import android.preference.PreferenceManager;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TableLayout;
 import android.widget.TableRow;
 import android.widget.TextView;
 import android.widget.Toast;

  public class IndividualViewFragment extends Fragment {


private static final String DB_TAG = "IndividualView";

private ImageView userImage;
private Person personToDisplay;
protected String numToCall, numtype;
//private DatabaseHelper db;



//private UnityMobileApp appCtx;


protected static final int DIALOG_VIEW_CALENDAR = 2;    

protected static final int MESSAGE_CONNECTION_ERROR_TOAST = 0;
//private static final String DEBUG_TAG = "unity.IndividualViewActivity";

private final Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MESSAGE_CONNECTION_ERROR_TOAST:
            Toast.makeText(getActivity(), Consts.CONNECTION_ERROR_MESSAGE, Toast.LENGTH_SHORT).show();
            break;
        }
    }
};
private SharedPreferences prefs;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = (LinearLayout) inflater.inflate(R.layout.individual_view, container, false);

    //db = new DatabaseHelper(appCtx);
   // appCtx = (UnityMobileApp) getActivity().getApplication();

    userImage = (ImageView)getView().findViewById(R.id.individualUserImage);
    return view;


}

public void onPause(){
    super.onPause();
}

public void onDestroy(){
    super.onDestroy();
    //doUnbindService();
    //db.close();
}

public void onResume(){
    super.onResume();

    //personToDisplay = appCtx.getEveryone().get(username);
    updateView();
    //updateInfo();

}


private void updateView(){

    userImage.setImageBitmap(personToDisplay.getRoundedImage(Consts.LARGE_USER_IMAGE_SIZE, Consts.LARGE_USER_IMAGE_BORDER));

}

}

我的清单文件在下面

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.fxpal.unity.android"
  android:versionCode="1"
  android:versionName="1.0" >

 <uses-sdk android:minSdkVersion="15" />
 <application
    android:icon="@drawable/unity_icon"
    android:label="@string/app_name" 
    android:name=".UnityMobileApp"
    android:theme="@android:style/Theme.Holo" android:logo="@drawable/fuji_xerox_logo_125">
    <activity
        android:label="@string/app_name"
        android:name=".MainActivity" >
         <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".IndividualViewActivity"
        android:label="@string/app_name" android:theme="@style/PageStyle">
    </activity>
    <service android:name=".ReportingService" />
    <receiver android:name=".StartupReceiver"/>
   </application>

   </manifest>

请帮我。我无法找到问题所在。

问候, 拉克什

4

1 回答 1

1

你的Adapter构造函数:

public EveryoneAdapter(Context c, UnityMobileApp a) {
    mContext = c;
    appCtx = a;
    mNamesList = new ArrayList<String>(a.everyone.keySet()); // Here you call a.everyone
}

但是当您调用该构造函数时:

gridView.setAdapter(new EveryoneAdapter( view.getContext(),appCtx)); // appCtx has not been defined

因此,在您的构造函数中,您实际上是在调用appCtx.everyone.keySet(). 由于appCtxnull(尚未由应用程序设置),它将崩溃。您需要appCtx正确创建或找到您的。

于 2012-07-23T20:36:03.827 回答