Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从OnClickListener调用的方法不更新成员变量_Java_Android_Eclipse_Onclicklistener - Fatal编程技术网

Java 从OnClickListener调用的方法不更新成员变量

Java 从OnClickListener调用的方法不更新成员变量,java,android,eclipse,onclicklistener,Java,Android,Eclipse,Onclicklistener,我的头撞在墙上了。。。我不明白为什么这不起作用。。。我有一个android应用程序,它基本上使用了三种活动:MainActivity、SummaryViewer和WebViewer。main活动基本上由用户可以按下的四个按钮组成。根据按下的按钮,SummaryViewer应该启动,相应的内容显示在SummaryViewer中的两个文本视图中。这四个按钮是“人”、“地点”、“事件”和“事物” SummaryViewer类的内容存储在四个不同的对象数组中,并具有相应的名称(即mPeople[]、mP

我的头撞在墙上了。。。我不明白为什么这不起作用。。。我有一个android应用程序,它基本上使用了三种活动:
MainActivity
SummaryViewer
WebViewer
main活动
基本上由用户可以按下的四个按钮组成。根据按下的按钮,
SummaryViewer
应该启动,相应的内容显示在
SummaryViewer
中的两个文本视图中。这四个按钮是“人”、“地点”、“事件”和“事物”

SummaryViewer
类的内容存储在四个不同的对象数组中,并具有相应的名称(即mPeople[]、mPlaces[]等)。在
MainActivity
中,我为四个按钮中的每一个设置了
onClickListener
。在每个
onClickLister
中,我都有一个对
setNumType
的调用,以及启动
SummaryViewer
活动的意图。在调用
SummaryViewer
时,每个按钮都执行相同的操作,但我将不同的整数传递给
setNumType
,具体取决于按下的按钮。因此,如果按下People按钮,我调用
setNumType(1)
,它将变量
numType
设置为“1”。如果按Places,我调用
setNumType(2)等等

以下是我的MainActivity类的代码:

public class MainActivity extends ActionBarActivity {

private Button placesButton; 
private Button peopleButton; 
private Button eventsButton; 
private Button thingsButton; 
private int type; 


public int getNumType(){
    return type; 
}

public void setNumType(int i){
    type = i; 
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    placesButton = (Button)findViewById(R.id.places_button); 
    placesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            setNumType(1);  
            Intent openSumViewerClass = new Intent("com.tierramaxis.interestingwikipedia.SUMMARYVIEWER"); 
            startActivity(openSumViewerClass);
        }
    });

    peopleButton = (Button)findViewById(R.id.people_button); 
    peopleButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            setNumType(2); 
            Intent openSumViewerClass = new Intent("com.example.SUMMARYVIEWER"); 
            startActivity(openSumViewerClass);
        }
    });

    eventsButton = (Button)findViewById(R.id.events_button); 
    eventsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            setNumType(3); 
            Intent openSumViewerClass = new Intent("com.exmaple.SUMMARYVIEWER"); 
            startActivity(openSumViewerClass);
        }
    });

    thingsButton = (Button)findViewById(R.id.things_button); 
    thingsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            setNumType(4); 
            Intent openSumViewerClass = new Intent("com.exmple.SUMMARYVIEWER"); 
            startActivity(openSumViewerClass);
        }
    });
}
SummaryViewer
中,我通过
MainActivity
的对象访问
numType
变量,并将其设置为
type
type
然后通过相应的方法传递给两个switch语句,并根据
type
的值设置TextView的内容。基本上,它根据
type
是1、2、3还是4来调用不同的对象数组。以下是SummaryViewer类中的相关代码:

public class SummaryViewer extends Activity {

private Button mNextButton; 
private Button mPrevButton; 
private Button mGoButton; 
private TextView mArticleTitle; 
private TextView mArticleSummary;

MainActivity numType = new MainActivity(); 

private int type = numType.getNumType();

//bunch of object arrays for content go in here...

private static int mCurrentIndex = 1;

private void updateTitle(){
    int title = 0; 

    switch (type){

    case 1: title = mInterestingPlaces[mCurrentIndex].getTitle(); 
    mArticleTitle.setText(title); 
    break;

    case 2: title = mInterestingPeople[mCurrentIndex].getTitle(); 
    mArticleTitle.setText(title); 
    break;

    case 3: title = mInterestingEvents[mCurrentIndex].getTitle(); 
    mArticleTitle.setText(title); 
    break;

    case 4: title = mInterestingThings[mCurrentIndex].getTitle(); 
    mArticleTitle.setText(title); 
    break;
    } 
};

private void updateSummary(){
    int summary = 0; 

    switch (type){

    case 1: summary = mInterestingPlaces[mCurrentIndex].getSummary(); 
    mArticleSummary.setText(summary); 
    break;

    case 2: summary = mInterestingPeople[mCurrentIndex].getSummary(); 
    mArticleSummary.setText(summary); 
    break;

    case 3: summary = mInterestingEvents[mCurrentIndex].getSummary(); 
    mArticleSummary.setText(summary); 
    break;

    case 4: summary = mInterestingThings[mCurrentIndex].getSummary(); 
    mArticleSummary.setText(summary); 
    break;
    } 
};

/*public static String updateURL(){
    // this works differently, so I've omitted it to avoid confusion.
};*/

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

    mArticleTitle = (TextView)findViewById(R.id.title);         
    mArticleSummary = (TextView)findViewById(R.id.summary); 

    updateTitle();
    updateSummary();


    mNextButton = (Button)findViewById(R.id.next_button); 
    mNextButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex +1) % mInterestingEvents.length; 
            updateTitle(); 
            updateSummary(); 
        }
    });

    mPrevButton = (Button)findViewById(R.id.prev_button); 
    mPrevButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex - 1); 
            if (mCurrentIndex < 0) mCurrentIndex = mInterestingEvents.length - 1;
            updateTitle();
            updateSummary(); 
        }
    });

    mGoButton = (Button)findViewById(R.id.go_button); 
    mGoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent openPageViewer = new Intent("com.example.PAGEVIEWER"); 
            startActivity(openPageViewer);
        }
    });
公共类摘要查看器扩展活动{
私用按钮;
私人按钮MPREV按钮;
专用按钮MGOBUTON;
私有文本视图标题;
私有文本视图摘要;
MainActivity numType=新的MainActivity();
private int type=numType.getNumType();
//内容的一堆对象数组放在这里。。。
私有静态int-mCurrentIndex=1;
私有void updateTitle(){
int title=0;
开关(类型){
案例1:title=mInterestingPlaces[mCurrentIndex].getTitle();
mArticleTitle.setText(标题);
打破
案例2:title=mInterestingPeople[mCurrentIndex].getTitle();
mArticleTitle.setText(标题);
打破
案例3:title=mInterestingEvents[mCurrentIndex].getTitle();
mArticleTitle.setText(标题);
打破
案例4:title=mInterestingThings[mCurrentIndex].getTitle();
mArticleTitle.setText(标题);
打破
} 
};
私有void更新摘要(){
int summary=0;
开关(类型){
案例1:summary=mInterestingPlaces[mCurrentIndex].getSummary();
mArticleSummary.setText(摘要);
打破
案例2:summary=mInterestingPeople[mCurrentIndex].getSummary();
mArticleSummary.setText(摘要);
打破
案例3:summary=mInterestingEvents[mCurrentIndex].getSummary();
mArticleSummary.setText(摘要);
打破
案例4:summary=mInterestingThings[mCurrentIndex].getSummary();
mArticleSummary.setText(摘要);
打破
} 
};
/*公共静态字符串updateURL(){
//这是不同的工作方式,所以我省略了它以避免混淆。
};*/
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(null);
setContentView(R.layout.viewer\u摘要);
mArticleTitle=(TextView)findViewById(R.id.title);
mArticleSummary=(TextView)findViewById(R.id.summary);
updateTitle();
更新摘要();
mNextButton=(按钮)findViewById(R.id.next_按钮);
mNextButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
mCurrentIndex=(mCurrentIndex+1)%mInterestingEvents.length;
updateTitle();
更新摘要();
}
});
mPrevButton=(按钮)findViewById(R.id.prev_按钮);
mPrevButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
mcurrentidex=(mcurrentidex-1);
如果(mCurrentIndex<0)mCurrentIndex=mInterestingEvents.length-1;
updateTitle();
更新摘要();
}
});
mGoButton=(按钮)findViewById(R.id.go_按钮);
mGoButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent openPageViewer=newintent(“com.example.PAGEVIEWER”);
startActivity(openPageViewer);
}
});
}

问题是当我在每个
onClickListener
中调用
setNumType
时,
numType
中没有设置
MainActivity
中,变量确实会被传递,因为
SummaryActivity
中的文本视图包含适当的内容。例如,如果我在
MainActivity
中初始化
private int numType=3
,则
SummaryViewer
中的内容与
mEvents
相对应,无论是哪一个按钮被按下

因此,在我看来,变量
numType
没有被设置,因为它应该基于
main活动中对
onClickListener
的每次调用


有人能帮我解决这个问题吗?我花了很长时间想弄明白。

要进一步解释@opiatefuch的答案:

已编辑

主要活动

Intent openSumViewerClass = new Intent(MainActivity.this, SummaryViewer.class);
openSumViewerClass.putExtra("numType", 2); 
startActivity(openSumViewerClass);
然后在总结六
mArticleTitle = (TextView)findViewById(R.id.title);         
mArticleSummary = (TextView)findViewById(R.id.summary); 

type = getIntent.getIntExtra("numType", 0);

updateTitle();
updateSummary();