当我点击第一个片段的按钮时,我试图将textview中的值增加1秒。没有IDE(eclipse)检测到错误。当我运行应用程序并单击按钮时,会出现一个对话框,说[不幸的是,苹果(我的应用程序名称)已经停止]。我google并发现我们应该使用界面进行交流。但我想知道我在哪里犯了错误。我正在应用的概念错误。请帮助我...
MainActivity.Java
package com.iphone.apple;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
MyFragment_2 ob;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ob = new MyFragment_2();
}
public void run(int count) {
ob.rockon(count);
}
}
FirstFragment
package com.iphone.apple;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class MyFragment extends Fragment implements OnClickListener {
int counter = 0;
Button mButton;
MainActivity ob;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.myfragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
mButton = (Button) getView().findViewById(R.id.button1);
mButton.setOnClickListener(this);
ob = new MainActivity();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
ob.run(counter);
}
}
SecondFragment
package com.iphone.apple;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment_2 extends Fragment {
TextView ob = null;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_2, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ob = (TextView) getView().findViewById(R.id.textview);
}
public void rockon(int count) {
ob.setText("count:"+count);
}
}
答案 0 :(得分:1)
ob = new MainActivity();
您必须永远不要自己实例化活动。这是Android框架的责任。