我无法导航到另一个活动

时间:2017-10-16 20:41:06

标签: android

Android Error 当谈到Android Studio时,我是一个真正的菜鸟,而我正试图这样做,当我点击IntroActivity中的一个按钮时,它会引导我进入一个包含一个列表和导航抽屉,如果这很重要。但是,当我第一次在模拟器中单击时,应用程序停止,当我返回并再次单击它时,没有任何反应。我有两个活动的Java代码。有人可以帮帮我吗?

<activity
android:name=".FeaturedActivity"
android:label="@string/title_activity_featured"
android:theme="@style/AppTheme.NoActionBar"></activity>

第一项活动

public class IntroActivity extends AppCompatActivity {

Button ComeInButton;

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

    ComeInButton = (Button) findViewById(R.id.ComeIn);
    ComeInButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent i = new Intent(IntroActivity.this, FeaturedActivity.class);
            startActivity(i);
        }
    });
}
}

特色活动

public class FeaturedActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

String [] art_Names;
TypedArray pics;
String[] artist_Names;
String[] desc;

List<RowItem> rowItems;
ListView myListView;

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

    rowItems=new ArrayList<RowItem>();
    art_Names=getResources().getStringArray(R.array.Featured_Arts);
    pics=getResources().obtainTypedArray(R.array.Art_Pics);
    artist_Names=getResources().getStringArray(R.array.Artist_Names);
    desc=getResources().getStringArray(R.array.Descriptions);

    for(int i=0;i<art_Names.length;i++){
        RowItem item=new RowItem(art_Names[i], pics.getResourceId(i, -1),artist_Names[i],desc[i]);
        rowItems.add(item);
    }

    myListView=(ListView)findViewById(R.id.list);
    CustomerAdapter adapter = new CustomerAdapter(this,rowItems);
    myListView.setAdapter(adapter);

    myListView.setOnClickListener((View.OnClickListener) this);





    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}





public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    String art_name=rowItems.get(position).GetArtName();
    Toast.makeText(getApplicationContext(),""+art_name,Toast.LENGTH_SHORT).show();
}




@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.featured, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

2 个答案:

答案 0 :(得分:0)

问题在细节上有点模糊,但在黑暗中拍摄,您是否在应用清单中注册了FeaturedActivity?

类似的东西:

<activity
        android:name=".FeaturedActivity"
        android:label="@string/title_activity_featured"
        android:theme="@style/AppTheme">

应该在您的AndroidManifest.xml文件中。

你可能在logcat中收到一条很好的错误信息,所以请看那里:)

答案 1 :(得分:0)

我认为崩溃原因是您在FeaturedActivity内投放View.OnClickListenerFeatureActivity#onCreate()的地方。由于它没有实现View.OnClickListener,因此您得到ClassCastException,因为您没有捕获此异常,那么您的应用程序崩溃了。

编辑我的意思是这一行:

public class FeaturedActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {            
        ...     
        myListView.setOnClickListener((View.OnClickListener) this); // this line
    }       
    ...
}

编辑2 您可以通过在View.OnClickListener中实施FeatureActivity来解决此问题:

public class FeaturedActivity extends AppCompatActivity implements
    NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {            
        ...     
        myListView.setOnClickListener(this); // cast is not needed anymore
    }

    @Override
    protected void onClick(View v) {
        // handle click
    }
    ...
}

顺便说一下,如果您要处理ListView的商品点击,则应使用Batch insert nodes and relations neo4jclient,以便在点击ListView中的某个商品时收到通知