1

我试图从一个片段中调用一个活动,但是当我在手机中运行它时应用程序停止了。如果我删除意图,它会显示 toast,但是当我尝试使用意图运行它时,它会停止。

我已经将活动添加到清单中。

package com.example.bar.adapter;

import com.example.bar.R;
import com.example.bar.zbarreader.CameraTestActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class Inventory extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        //inflate the right layout
        View rootView = inflater.inflate(R.layout.inventory, container, false);

        // Define and execute a button
        Button InvScan = (Button) rootView.findViewById(R.id.InvScan);
        InvScan.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) { 
                // Show a toast
                Toast.makeText(getActivity(), "Openning the Barcode Reader", Toast.LENGTH_SHORT).show();

                // Call the Activity
                Intent IntentScanner = new Intent(getActivity(), CameraTestActivity.class);
                Log.i(getTag(), getTag());
                startActivity(IntentScanner);

            }
        });

        return rootView;

    }

}
4

3 回答 3

0

那必须有效:

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    Intent nextScreen = new Intent(getApplicationContext(), newActivity.class);
    startActivity(nextScreen);
    finish();
        }
    });

我希望这会帮助你:)

于 2014-01-30T07:24:16.830 回答
0

对于您要打开的每个新 Activity,您必须在AndroidManifest.xml中编写以下内容:

  </activity> 
        <activity android:name=".CameraTestActivity"></activity>
        <activity android:name=".ImageTestActivity"></activity>
    </application>

我希望这会帮助你:)

于 2014-01-30T07:19:43.730 回答
0

尝试这个

// Call the Activity
 Intent IntentScanner = new Intent(getActivity(), CameraTestActivity.class);
 Log.i(getTag(), getTag()); 
 getActivity().startActivity(IntentScanner);

getTag()或者in ...一定有问题,Log请发布您的getTag().

于 2014-01-30T07:08:49.757 回答