更改为横向模式时应用程序崩溃

时间:2014-04-10 21:29:54

标签: java android android-fragments

如果我在横向模式下启动应用程序它运行正常,但如果我 从横向更改模式或配置文件崩溃。 谁能在我的代码中看到一些明显的错误? 我试图在清单中只设置纵向模式,但没有成功。

代码:

   public class MainActivity extends Activity implements OnClick {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_timeline, new TimeLineFragment());
    transaction.commit();

}

@Override
public void OnClickListener(Status status) {

    if(findViewById(R.id.fragment_detailedtweet) != null) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("StatusData", status);
        DetailTweetFragments detailTweetFragment = new DetailTweetFragments();
        detailTweetFragment.SetStatus(status);
        detailTweetFragment.setArguments(bundle);


        FragmentTransaction trancationTableView = getFragmentManager().beginTransaction();
        trancationTableView.replace(R.id.fragment_detailedtweet, detailTweetFragment);
        trancationTableView.commit();

    } else {
        Bundle bundle = new Bundle();
        bundle.putSerializable("StatusData", status);
        DetailTweetFragments detailTweetFragment = new DetailTweetFragments();
        detailTweetFragment.SetStatus(status);
        detailTweetFragment.setArguments(bundle);

        FragmentTransaction trancationTableView = getFragmentManager().beginTransaction();
        trancationTableView.replace(R.id.fragment_timeline, detailTweetFragment);
        trancationTableView.addToBackStack(null);
        trancationTableView.commit();
    }

}


@Override
public void OnMakeTweet() {
    Intent intent = new Intent(MainActivity.this, TwitterUpdateActivity.class);
    startActivity(intent);

}

@Override
public void OnProfile() {
    Intent intent = new Intent(MainActivity.this, TwitterProfileActivity.class);
    startActivity(intent);

}
}

    public class TimeLineAdapter extends ArrayAdapter<Status>{

private Activity activityContext;
public TimeLineAdapter(Context context) {
    super(context, R.layout.status_item);
    activityContext = (Activity) context;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    Status status = this.getItem(position);

    View view = activityContext.getLayoutInflater().inflate(R.layout.status_item, null);

    TextView tweetText = (TextView) view.findViewById(R.id.tweet_from_timeLine);
    tweetText.setText(status.getText());

    TextView profileName = (TextView) view.findViewById(R.id.profile_name_status);
    profileName.setText(status.getUser().getName());

    LinearLayout layout = (LinearLayout) view.findViewById(R.id.left_layout);

    ImageView imageView = new ImageView(getContext());

    layout.addView(imageView);

    public class TimeLineFragment extends Fragment {

Button timeLine_button;
Button makeTweet_button;
Button profile_button;
TimeLineAdapter adapter; 
OnClick onClickListener;


public ResponseList<twitter4j.Status> statuses;

public interface OnClick {
    public void OnClickListener(Status status);

    public void OnMakeTweet();
    public void OnProfile();
}



@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        onClickListener = (OnClick) activity;
    }catch(ClassCastException e){
        e.printStackTrace();
    }
}

@Override
public void onDetach() {
    super.onDetach();
    onClickListener = null;

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {



    View view = inflater.inflate(R.layout.fragment_timeline, null);


    ListView list = (ListView) view.findViewById(R.id.tweet_list);


    list.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> adapter, View praent, int position,
                long id) {
            onClickListener.OnClickListener(statuses.get(position));

        }
    });


    makeTweet_button = (Button) view.findViewById(R.id.makeTweet_button);
    makeTweet_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            onClickListener.OnMakeTweet();
        }
    });

    profile_button = (Button) view.findViewById(R.id.profile_button);
    profile_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            onClickListener.OnProfile();
        }
    });

    new LoadTmeLine().execute();

    return view; 

}




private class LoadTmeLine extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        Twitter twitter = TwitterLogInActivity.twitter;
        try {
            statuses = twitter.getHomeTimeline();
        } catch (TwitterException e) {

            e.printStackTrace();
        }

        return null;
    }
    @Override
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);

        adapter = new TimeLineAdapter(getActivity());
        for(twitter4j.Status status : statuses) {
            adapter.add(status);
        }

        ListView list = (ListView) getActivity().findViewById(R.id.tweet_list);
        list.setAdapter(adapter);


    }
}

例外:

  FATAL EXCEPTION: main
  java.lang.NullPointerException
  at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
  at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:104)
  .adapters.TimeLineAdapter.<init>(TimeLineAdapter.java:31)
  .fragments.TimeLineFragment$LoadTmeLine.onPostExecute(TimeLineFragment.java:132)
  .fragments.TimeLineFragment$LoadTmeLine.onPostExecute(TimeLineFragment.java:1)
   at android.os.AsyncTask.finish(AsyncTask.java:631) 
  at android.os.AsyncTask.access$600(AsyncTask.java:177)
  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5103)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 at dalvik.system.NativeStart.main(Native Method)

清单:

    application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="twitter.TwitterLogInActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="twitter.MainActivity"
        android:label="@string/app_name" >
    </activity>

2 个答案:

答案 0 :(得分:0)

当方向发生变化时,活动将被终止并重新创建。当您没有正确保存状态时,通常会得到这样的空指针。至于强制应用程序为纵向模式。这应该做到。

<activity
     android:name=".MyActivity"
     android:label="@string/app_name"
     android:screenOrientation="portrait" />

答案 1 :(得分:0)

如果布局有不同的文件夹(横向为layout-land,正常情况为layout),请确保它们都具有相同的视图。有时我碰巧在普通布局中添加了一个新视图,但忘了将它添加到横向布局中