gpt4 book ai didi

java - 如何从 Android 应用程序中的另一个类调用非静态方法

转载 作者:行者123 更新时间:2023-12-02 03:28:36 24 4
gpt4 key购买 nike

所以我试图在 Android 应用程序中点击某个项目后调用一个方法。

这是我要调用的方法 getFavourites();

public class Favourites extends Activity {
public void getFavourites(){
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setDisplayedChild(0);
setTitle("Favourites");
}
}

这里是我要调用方法getFavourtites();

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

if (id == R.id.nav_favourite) {
Favourites getFav = new Favourites();
getFav.getFavourites();
} else if (id == R.id.nav_search) {
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setDisplayedChild(1);
setTitle("Search");
} else if (id == R.id.nav_share) {

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

}

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

我真的不知道我做错了什么,但每次运行它时,应用程序都会崩溃。

编辑:这是 Collection 夹的完整堆栈

package com.ctt.mrdan.croydontramtime2;

import android.app.Activity;
import android.widget.ViewFlipper;

/**
* Created by MrDan on 16/07/2016.
*/
public class Favourites extends Activity {
public void getFavourites(){
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setDisplayedChild(0);
setTitle("Favourites");
}
}

这是主程序的完整堆栈

package com.ctt.mrdan.croydontramtime2;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
}

@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.main, 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_favourite) {
Favourites getFav = new Favourites();
getFav.getFavourites();
} else if (id == R.id.nav_search) {
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setDisplayedChild(1);
setTitle("Search");
} else if (id == R.id.nav_share) {

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

}

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

最佳答案

您正在实例化 Favourites,然后在新实例上调用 getFavourites()getFavourites() 调用 findViewById(),后者隐式调用 getWindow().findViewById(),但 Activity没有内容 View ,导致 NullPointerException

Collection 夹根本没有任何目的。

您可以将代码从 getFavourites() 移动到 onNavigationItemSelected():

viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);

// ...

if (id == R.id.nav_favourite) {
viewFlipper.setDisplayedChild(0);
setTitle("Favourites");
} else if (id == R.id.nav_search) {
viewFlipper.setDisplayedChild(1);
setTitle("Search");
}

此外,您永远不要使用 new 关键字实例化 Activity。使用 Intent 并将实例化留给 ActivityManager

关于java - 如何从 Android 应用程序中的另一个类调用非静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38414026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com