簡體   English   中英

一個按鈕的簡單android應用:“不幸的是-app-已停止”

[英]Simple android app with one button: “unfortunately -app- has stopped”

我是Android的新手。 我只需要一個執行任務的按鈕,但是Internet上的示例代碼不起作用。 即使eclipse沒有給出任何錯誤,我也無法在設備上運行此應用。 這是代碼:

package com.example.myfirstapp;

import com.example.myfirstapp.R;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

@SuppressLint("ValidFragment")
public class MainActivity extends ActionBarActivity {
     static Button btn;

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment implements View.OnClickListener {

        public PlaceholderFragment() {
        }

          public void onClick(View v){
              Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sicioldrart.com"));
                startActivity(browserIntent);






            }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            btn=(Button) getView().findViewById(R.id.bottone);
            btn.setOnClickListener(this);



            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

謝謝您的幫助

您需要先放大視圖,然后才能找到按鈕。

移動:

 btn=(Button) getView().findViewById(R.id.bottone);
        btn.setOnClickListener(this);

下面

View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);

和更改getView()rootView

btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);

這兩行將

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

並用rootView替換getView()

btn=(Button) rootView.findViewById(R.id.bottone);
btn.setOnClickListener(this);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM