java.lang.UnsupportedOperationException运行时错误

时间:2015-10-01 22:55:49

标签: java android exception

我正在开发QR阅读器应用程序。在本课程中,我实施了在用户扫描QR码时将数据输入列表视图。当我运行它时,它给出UnsupportedOperationException。 Il在这里发布代码和日志。在此先感谢:)

public class QRScanMenu extends Activity {
ListView listView;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
private int reqCode= 0;

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

    HandleClick handleClick = new HandleClick();
    findViewById(R.id.QRButton).setOnClickListener(handleClick);
    findViewById(R.id.ExitButton).setOnClickListener(handleClick);
    listView = (ListView)findViewById(R.id.qrItmList);


}

private class HandleClick implements View.OnClickListener{
    public void onClick(View arg0){
        try{
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            switch (arg0.getId()){
                case R.id.QRButton:
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    break;

                case R.id.ExitButton:
                    Intent i = new Intent(QRScanMenu.this, MainActivity.class);
                    startActivity(i);
                    break;
            }
            startActivityForResult(intent, reqCode);
        }
        catch (ActivityNotFoundException ex){
            showDialog(QRScanMenu.this, "No Scanner Application Found", "Download Scanner Application?", "Yes", "No").show();
        }
    }
}

private static AlertDialog showDialog(final Activity activity, CharSequence title, CharSequence message, CharSequence btnYes, CharSequence btnNo){
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
    downloadDialog.setTitle(title);
    downloadDialog.setTitle(message);
    downloadDialog.setPositiveButton(btnYes, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
                activity.startActivity(intent);
            } catch (ActivityNotFoundException ex) {

            }
        }
    });
    downloadDialog.setNegativeButton(btnNo, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    return downloadDialog.show();
}

public void onActivityResult(int reqCode, int resCode, Intent intent){
    super.onActivityResult(reqCode, resCode, intent);
    if (reqCode == 0){
        TextView qrStatus = (TextView) findViewById(R.id.statusText);
        TextView qrResult = (TextView) findViewById(R.id.resultText);

        if(resCode == RESULT_OK){
            qrStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
            qrResult.setText(intent.getStringExtra("SCAN_RESULT"));
            String scanRes = intent.getStringExtra("SCAN_RESULT");

            String[]result = scanRes.split(":");
            HashMap<String, String> resItems = new HashMap<String, String>();
            if (result.length>0){
                resItems.put("itmName", result[0]);
                getItmPrice(result[1], resItems);
            }

            list.add(resItems);
            addListView();

        }
        else if (resCode == RESULT_CANCELED){
            qrStatus.setText("Press QR Scan button to Scan Item");
            qrResult.setText("Scan Cancelled");
        }
    }
}

private String getItmPrice(String value, HashMap<String, String> resItems) {
    return resItems.put("itmPrice", value);
}

private void addListView(){
    SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.qrscannermenu, new String[]{"itmName", "itmPrice"}, new int[]{R.id.text1, R.id.text2});
    listView.setAdapter(simpleAdapter);
}
}

这里是Log

    10-02 04:22:30.790  11800-11800/supprioritizer.warnerit.com.supermarketprioritizer E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: supprioritizer.warnerit.com.supermarketprioritizer, PID: 11800
    java.lang.RuntimeException: Unable to start activity ComponentInfo{supprioritizer.warnerit.com.supermarketprioritizer/supprioritizer.warnerit.com.supermarketprioritizer.QRScanMenu}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2431)
            at android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5309)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
            at android.widget.AdapterView.addView(AdapterView.java:487)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:430)
            at android.app.Activity.setContentView(Activity.java:2414)
            at supprioritizer.warnerit.com.supermarketprioritizer.QRScanMenu.onCreate(QRScanMenu.java:33)
            at android.app.Activity.performCreate(Activity.java:6865)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2431)
   at 
android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5309)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

XML文件

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linLayout1">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/QRButton"
            android:text="QR Scan"
            android:textSize="18sp"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ExitButton"
            android:text="Back Main"
            android:textSize="18sp"/>
    </LinearLayout>


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/statusText"
        android:layout_below="@+id/linLayout1"
        android:text="Press QR Scan Button to Scan Items"
        android:textSize="18sp"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/resultText"
        android:text="Ready to Scan"
        android:textSize="18sp"
        android:layout_below="@+id/statusText"
        android:background="@android:color/white"
        android:textColor="@android:color/black"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/qrItmList">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/text1"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/text2"/>
        </ListView>

    </LinearLayout>

    </RelativeLayout>

enter link description here

1 个答案:

答案 0 :(得分:0)

你的问题在于:

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/qrItmList">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text2"/>
    </ListView>

ListView不能以这种方式声明孩子。您可以通过ListView(在您的情况下为Adapter)填充SimpleAdapter。删除这两个TextView小部件,或将它们移动到布局中的其他位置。