如何显示第二个活动(表格)

时间:2015-08-06 13:26:46

标签: android android-intent

我想展示第二项活动(表格)。我使用此代码显示第二个活动。

//代码

Intent intent=new Intent(v.getContext(),MainActivity2Activity.class);
startActivityForResult(intent,0);

如果我没有添加我的方法,这段代码工作正常。当我点击按钮显示第二个活动或第一个活动它工作正常,但问题,当我添加我的方法我得到错误说“不幸的应用程序名称已停止”。

//我的第一个活动代码是。

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn1=(Button)findViewById(R.id.btnsignup);
    btn1.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    Intent intent=new Intent(v.getContext(),MainActivity2Activity.class);
    startActivityForResult(intent,0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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("unused")
private static final String PHOTO_BASE_URL="";
TextView OutPut;
ProgressBar pg;
List<MyTask> tasks;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn1=(Button)findViewById(R.id.btnPost);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startCall();
        }


    });

    OutPut=(TextView)findViewById(R.id.textView);
    OutPut.setMovementMethod(new ScrollingMovementMethod());

    pg =(ProgressBar)findViewById(R.id.progressBar);
    pg.setVisibility(View.INVISIBLE);

    tasks=new ArrayList<>();


}

private void startCall() {
   // if(isOnline()){
        requestData("http://10.0.2.2/android.php");
    //}else{
    //    Toast.makeText(this, "Network is not available", Toast.LENGTH_LONG).show();
    //}
}

private void requestData(String url) {
    // updateDisplay("Done it");
    RequestPackage p=new RequestPackage();
    p.setMethod("GET");
    //p.setMethod("POST");
    p.setUri(url);
    p.setParam("name1", "makame haji 1");
    p.setParam("name2","makame haji 2");
    p.setParam("name3","makame haji 3");

    MyTask task = new MyTask();
    task.execute(p);
}



protected boolean isOnline(){
    ConnectivityManager cm=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfor = cm.getActiveNetworkInfo();
    if(netInfor != null && netInfor.isConnectedOrConnecting()){
        return true;
    }else{
        return false;
    }
}

private  class  MyTask extends AsyncTask<RequestPackage,String,String>{

   @Override
    protected void onPreExecute(){
       // updateDisplay("Started it");
        if(tasks.size()==0){
            pg.setVisibility(View.VISIBLE);
        }
        tasks.add(this);
    }

    @Override
    protected  String doInBackground(RequestPackage... params){
        String contect = HttpManager.getData(params[0]);
        // use this for pawword
        //String contect = HttpManager.getData_By_PASSWORD(params[0],"username","password");
        /*flowersList=FlowerJSONParser.parseFeed(contect);

        for(Flowers flowers:flowersList){
            try{
                String imageUrl=PHOTO_BASE_URL+flowers.getBitmap();
                InputStream in=(InputStream) new URL(imageUrl).getContent();
                Bitmap bitmap= BitmapFactory.decodeStream(in);
                flowers.setBitmap(bitmap);
                in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }*/
        return  contect;
    }

   @Override
    protected  void  onPostExecute(String result){
       //flowersList= FlowerJSONParser.parseFeed(result);
       //flowersList= persers.parseFeed(result);
        updateDisplay(result);

       tasks.remove(this);
       if(tasks.size()==0){
            pg.setVisibility(View.INVISIBLE);
        }
    }

   @Override
    protected  void onProgressUpdate(String... values){
        //updateDisplay(values[0]);
    }
}

}

请任何人帮助我

2 个答案:

答案 0 :(得分:0)

检查是否在Manifest中添加了互联网权限:

<uses-permission android:name="android.permission.INTERNET/>

另请打印错误的完整堆栈跟踪

答案 1 :(得分:0)

你有任何日志给我们吗?请。

你应该使用原生的东西:

Button mButton = (Button) findViewById(R.id.your_button_id_in_the_layout);
Log.d(TAG, " onCreate");
mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, DummyActivity.class);
        startActivity(intent);
    }
});