尝试打开detailactivity时发生意外崩溃

时间:2018-08-09 13:54:49

标签: java android

我的材料设计应用程序面临一个奇怪的问题。有些缩略图正在按预期方式打开和加载详细信息活动,但有些缩略图没有打开,而是发生了崩溃。在此视频中,您可以看到我面临的问题。 我将此链接附加到我的项目ZIP文件链接中,My Project

这是主要活动....

for

} 这是细节活动……

public class MainActivity extends AppCompatActivity implements ReaderAdapter.ReaderOnClickItemHandler {
public final static String READER_DATA = "reader";
public final static String POSITION = "position";
private final static String TAG = MainActivity.class.getSimpleName();
private static final String SAVED_ARRAYLIST = "saved_array_list";
private static final String SAVED_LAYOUT_MANAGER = "layout-manager-state";
private ApiInterface mApiInterface;
private List<Reader> mNetworkDataList;
@BindView(R.id.main_recycler_view)
RecyclerView mRecyclerView;
@BindView(R.id.main_linear_layout)
LinearLayout mErrorLinearLayout;
@BindView(R.id.main_progress_bar)
ProgressBar mProgressBar;
@BindView(R.id.toolbar_main)
Toolbar toolbar;
@BindView(R.id.main_reload_button)
Button mButton;
private ReaderAdapter mReaderAdapter;
private Parcelable onSavedInstanceState = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    if (null != toolbar) {
        setSupportActionBar(toolbar);
        toolbar.setTitle(getResources().getString(R.string.app_name));
    }
    mApiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    mReaderAdapter = new ReaderAdapter(this, this);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
            LinearLayoutManager.VERTICAL, false);
    mRecyclerView.setAdapter(mReaderAdapter);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    // getting the data from api using retrofit interface ApiInterface
    if (savedInstanceState != null) {
        onSavedInstanceState = savedInstanceState.getParcelable(SAVED_LAYOUT_MANAGER);
        mNetworkDataList = savedInstanceState.getParcelableArrayList(SAVED_ARRAYLIST);
    }
    if (null == mNetworkDataList) {

        loadData();

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData();
            }
        });
    }else {
        loadAdapter();
    }
}

public void loadData() {
    final Call<List<Reader>> listCall = mApiInterface.getAllReaderData();
    // now binding the data in the pojo class
    listCall.enqueue(new Callback<List<Reader>>() {
        //if data is successfully binded from json to the pojo class onResponse is called
        @Override
        public void onResponse(Call<List<Reader>> call,
                               Response<List<Reader>> response) {

            Log.d(TAG, "Response : " + response.code());
            mNetworkDataList = response.body();
            loadAdapter();
        }

        //if data binding is not successful onFailed called
        @Override
        public void onFailure(Call<List<Reader>> call, Throwable t) {
            //cancelling the GET data request
            listCall.cancel();
            showError();
        }
    });
}

private void loadAdapter() {
    if (null != mNetworkDataList) {
        showReaderList();
        mReaderAdapter.ifDataChanged(mNetworkDataList);
        if (onSavedInstanceState != null) {
            mRecyclerView.getLayoutManager().onRestoreInstanceState(onSavedInstanceState);
        }
    }
}

/**
 * this method is for showing the error textview and making all other views gone
 */
private void showError() {
    mRecyclerView.setVisibility(View.GONE);
    mProgressBar.setVisibility(View.GONE);
    mErrorLinearLayout.setVisibility(View.VISIBLE);
}

/**
 * this method is for showing the recyclerview and making all other views gone
 */
private void showReaderList() {
    mRecyclerView.setVisibility(View.VISIBLE);
    mProgressBar.setVisibility(View.GONE);
    mErrorLinearLayout.setVisibility(View.GONE);
}




private int numberOfColumns() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    // You can change this divider to adjust the size of the poster
    int widthDivider = 400;
    int width = displayMetrics.widthPixels;
    int nColumns = width / widthDivider;
    if (nColumns < 2) return 2;
    return nColumns;
}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable(SAVED_LAYOUT_MANAGER, mRecyclerView.getLayoutManager()
            .onSaveInstanceState());
    if (mNetworkDataList != null)
        outState.putParcelableArrayList(SAVED_ARRAYLIST, new ArrayList<Parcelable>(mNetworkDataList));

}

@Override
public void onClickItem(int position, Reader reader, ImageView mImage, TextView mTitle) {
    // Check if we're running on Android 5.0 or higher

    Intent readerIntent = new Intent(this, ReaderDetailsActivity.class);
    Bundle mBundle = new Bundle();
    mBundle.putParcelable(READER_DATA, reader);
    mBundle.putInt(POSITION, position);
    readerIntent.putExtras(mBundle);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Apply activity transition
        ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
                this,
                // Now we provide a list of Pair items which contain the view we can transitioning
                // from, and the name of the view it is transitioning to, in the launched activity
                new Pair<View, String>(mImage,
                        ReaderDetailsActivity.VIEW_NAME_HEADER_IMAGE),
                new Pair<View, String>(mTitle,
                        ReaderDetailsActivity.VIEW_NAME_HEADER_TITLE));
        ActivityCompat.startActivity(this, readerIntent, activityOptions.toBundle());

    } else {
        // Swap without transition
        startActivity(readerIntent);
    }

}

}

Json link 我正在为此项目进行解析。

这是我的项目的屏幕录像,您可以在其中看到我所面临的问题,recording

这是我尝试调试时的控制台日志。 运行正常时,控制台日志为public class ReaderDetailsActivity extends AppCompatActivity { private static final String TAG = ReaderDetailsActivity.class.getSimpleName(); private static final String SAVED_ARRAYLIST = "saved_array_list"; private static final String SAVED_LAYOUT_MANAGER = "layout-manager-state"; private final static String ARTICLE_SCROLL_POSITION = "article_scroll_position"; // View name of the header image. Used for activity scene transitions public static final String VIEW_NAME_HEADER_IMAGE = "detail:header:image"; // View name of the header title. Used for activity scene transitions public static final String VIEW_NAME_HEADER_TITLE = "detail:header:title"; private int position; private Reader reader; private int[] scrollPosition = null; @BindView(R.id.scrollView_details) ScrollView mScrollView; @BindView(R.id.details_fragment_title) TextView mTitle; @BindView(R.id.imageView_details) ImageView mImageView; @BindView(R.id.textView_author_details) TextView mAuthor; @BindView(R.id.textView_published_date) TextView mPublishDate; @BindView(R.id.textView_description) TextView mDescription; @BindView(R.id.floatingActionButton_Up) FloatingActionButton mFloatingActionButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reader_details); ButterKnife.bind(this); Bundle bundle = getIntent().getExtras(); position=0; mFloatingActionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mScrollView.scrollTo(0,0); } }); ViewCompat.setTransitionName(mImageView, VIEW_NAME_HEADER_IMAGE); ViewCompat.setTransitionName(mTitle, VIEW_NAME_HEADER_TITLE); if (null != bundle) { position = bundle.getInt(MainActivity.POSITION); reader = bundle.getParcelable(MainActivity.READER_DATA); if(null != reader) { mTitle.setText(reader.getTitle()); mPublishDate.setText(reader.getPublishedDate()); mAuthor.setText(reader.getAuthor()); GlideApp.with(this) .load(reader.getPhoto()) .into(mImageView); mDescription.setText(reader.getBody()); } } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); scrollPosition = savedInstanceState.getIntArray(ARTICLE_SCROLL_POSITION); if (scrollPosition != null) { mScrollView.postDelayed(new Runnable() { public void run() { mScrollView.scrollTo(scrollPosition[0], scrollPosition[0]); } }, 0); } }

当崩溃时,控制台日志为

08/09 20:31:31: Launching app No apk changes detected since last installation, skipping installation of /home/soumyajit/AndroidStudioProjects/MaterialReader/app/build/outputs/apk/debug/app-debug.apk $ adb shell am force-stop lordsomen.android.com.materialreader $ adb shell am start -n "lordsomen.android.com.materialreader/lordsomen.android.com.materialreader.activities.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D Connecting to lordsomen.android.com.materialreader Connected to the target VM, address: 'localhost:8601', transport: 'socket' 预先感谢..

1 个答案:

答案 0 :(得分:0)

最大可包裹尺寸不能超过1mb。在您的应用中为2.1 Mb。在不将应用程序日期传递给下一个活动的情况下,您可以尝试传递商品ID并在下一个活动中加载数据。否则,您可以缓存列表数据,并且可以在详细信息活动中从本地数据库加载数据。如果您在android studio中看不到崩溃日志,因为它设置为“仅显示选定的活动”。在这种情况下,应用会关闭,然后此类日志不会显示在android studio中。将其切换为“无过滤器”,就可以看到所有日志。