1

我正在創建一個應用程序,該應用程序使用用戶指南的材質幻燈片,並在應用程序啓動時彈出,但我無法從幻燈片傳輸到我的activity_main佈局。使用setContentView時,即使屏幕也顯示爲空白。無法從素材介紹過渡到主要活動佈局

protected void onCreate(Bundle savedInstanceState) { 

    setFullscreen(true); 
    super.onCreate(savedInstanceState); 

    addSlide(new SimpleSlide.Builder() 
      .title(R.string.title) 
      .description(R.string.description) 
      .image(R.mipmap.icon) 
      .background(R.color.background) 
      .backgroundDark(R.color.background_dark) 
      .build()); 

    setContentView(R.layout.activity_main); 
} 

The github link to the dependencies

MainActivity代碼

public class MainActivity extends AppCompatActivity { 
private static final String TAG = "MainActivity"; 

private TaskDbHelper mHelper; 
private ListView mTaskListView; 
private ArrayAdapter<String> mAdapter; 


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

    /*SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 

     if(!prefs.getBoolean("new1",false)){ 

     //Only 1st time run code here 

     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putBoolean("new1", true); 
     editor.commit(); 
    }*/ 

    setContentView(R.layout.activity_main); 
    mHelper = new TaskDbHelper(this); 
    mTaskListView = (ListView) findViewById(R.id.list_todo); 
    updateUI(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main_menu, menu); 
    return super.onCreateOptionsMenu(menu); 
} 


@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_add_task: 

     final EditText taskEditText = new EditText(this); 
     AlertDialog dialog = new AlertDialog.Builder(this) 
       .setTitle("Add new task") 
       // .setMessage("What do you want to do next?") 
       .setView(taskEditText) 
       .setPositiveButton("Add", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         String task = String.valueOf(taskEditText.getText()); 
         if(task != null && !task.isEmpty()) { 
          SQLiteDatabase db = mHelper.getWritableDatabase(); 
          ContentValues values = new ContentValues(); 
          values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task); 
          db.insertWithOnConflict(TaskContract.TaskEntry.TABLE, 
            null, 
            values, 
            SQLiteDatabase.CONFLICT_REPLACE); 
          updateUI(); 
          db.close(); 
         } else { 

         } 
        } 
       }) 
       .setNegativeButton("Cancel", null) 
       .create(); 
     dialog.show(); 

     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

private void updateUI() { 
    ArrayList<String> taskList = new ArrayList<>(); 
    SQLiteDatabase db = mHelper.getReadableDatabase(); 
    Cursor cursor = db.query(TaskContract.TaskEntry.TABLE, 
      new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE}, 
      null, null, null, null, null); 
    while (cursor.moveToNext()) { 
     int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE); 
     taskList.add(cursor.getString(idx)); 
    } 

    if (mAdapter == null) { 
     mAdapter = new ArrayAdapter<>(this, 
       R.layout.item_todo, 
       R.id.task_title, 
       taskList); 
     mTaskListView.setAdapter(mAdapter); 
    } else { 
     mAdapter.clear(); 
     mAdapter.addAll(taskList); 
     mAdapter.notifyDataSetChanged(); 
    } 

    cursor.close(); 
    db.close(); 
} 

public void deleteTask(View view) { 
    View parent = (View) view.getParent(); 
    TextView taskTextView = (TextView) parent.findViewById(R.id.task_title); 
    String task = String.valueOf(taskTextView.getText()); 
    SQLiteDatabase db = mHelper.getWritableDatabase(); 
    db.delete(TaskContract.TaskEntry.TABLE, 
      TaskContract.TaskEntry.COL_TASK_TITLE + " = ?", 
      new String[]{task}); 
    db.close(); 
    updateUI(); 
} 

}

回答

0

,因爲你必須設置一個按鈕,完成介紹,並讓你的主要活動。 你不需要

setContentView(R.layout.activity_main); 

你必須把所有的代碼比主要活動等單獨的類,否則你不能去那裏。

以下是從庫示例代碼你提到

addSlide(new SimpleSlide.Builder() 
        .title(R.string.title_material_bold) 
        .description(R.string.description_material_bold) 
        .image(R.drawable.art_material_bold) 
        .background(R.color.color_material_bold) 
        .backgroundDark(R.color.color_dark_material_bold) 
        .scrollable(scrollable) 
        .buttonCtaLabel("finish") 
        .buttonCtaClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) 
         { 
          Intent main=new Intent(MainIntro.this,MainActivity.class); 
          startActivity(main); 
         } 
        }) 
        .build()); 

更多信息請參閱本類

MainIntroActivity.java

+0

用於it.Unable從活動MainIntroActivity到MainActivity切換,應用程序崩潰, –

+0

' 'Appmanifest code –

+0

could you post your log cat ? – skydroid