在android中加载Widget时出错

时间:2013-12-28 16:17:11

标签: android android-widget

我有一个基本的小部件使用我的代码为一个textView,然后我尝试添加更多的文本视图和格式化小部件都很好。然后我尝试添加小部件,并在我的小部件中收到问题加载小组件消息。

我得到的错误是:

12-28 11:05:11.896    1147-1147/com.teslacoilsw.launcher W/AppWidgetHostView﹕ updateAppWidget couldn't find any view, using error view
    android.view.InflateException: Binary XML file line #51: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:620)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.widget.RemoteViews.apply(RemoteViews.java:2390)
            at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:408)
            at o.fl.D(:381)
            at o.fl.updateAppWidget(:636)
            at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:439)
            at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:124)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.widget.RemoteViews.apply(RemoteViews.java:2390)
            at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:408)
            at o.fl.D(:381)
            at o.fl.updateAppWidget(:636)
            at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:439)
            at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:124)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: The android:onClick attribute cannot be used within a restricted context
            at android.view.View.<init>(View.java:3641)
            at android.widget.TextView.<init>(TextView.java:663)
            at android.widget.TextView.<init>(TextView.java:653)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.widget.RemoteViews.apply(RemoteViews.java:2390)
            at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:408)
            at o.fl.D(:381)
            at o.fl.updateAppWidget(:636)
            at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:439)
            at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:124)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)

我的AppWidgetProvider是:

public class HelloWidget extends AppWidgetProvider {

    @Override
    public void onEnabled(Context context){

        Log.d("widgetTest", "inside on enabled");

        //build url for async task
        String url = "http://api.brewerydb.com/v2/beer/random?key=0cdb22f84315834cc807c0176a927060&format=json&hasLabels=y";


        //call async task
        new GetRandomBeer(context).execute(url);



    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){

        //todo: call update code, which should be the same as onEnabled
        String url = "myHIddenUrl";

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_test);

        //call async task
        new GetRandomBeer(context).execute(url);


    }


}

我的Widget布局xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/widget_bg_normal"
    >

    <TextView
        android:id="@+id/beerTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView android:id="@+id/image"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_margin="10dip"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1">

            <TextView
                android:id="@+id/beerBreweryTitle2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="10sp"
                android:text="From: "
                android:textStyle = "bold"
                android:padding="5dip"

                ></TextView>

            <TextView
                android:id="@+id/beerBreweryName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="15sp"
                android:textColor="@color/orange"
                android:textStyle = "bold"
                android:padding="5dip"
                android:onClick="viewBrewery"
                android:clickable="true"

                ></TextView>

            <TextView
                android:id="@+id/beerStyleTitle2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="10sp"
                android:text="Style: "
                android:textStyle = "bold"
                android:padding="5dip"

                ></TextView>


            <TextView
                android:id="@+id/beerStyleName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:textSize="15sp"
                android:textColor="@color/orange"
                android:textStyle = "bold"
                android:padding="5dip"
                android:onClick="viewStyle"
                android:clickable="true"

                ></TextView>






        </LinearLayout>


    </LinearLayout>

    <TableLayout

        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:shrinkColumns="*"
        android:stretchColumns="*">

        <TableRow
            android:id="@+id/tableStatTitles"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:padding="5dip" >

            <TextView
                android:id="@+id/abvTitle"
                android:text="ABV"
                android:gravity="center"
                android:textStyle = "bold"
                android:textSize="10sp"
                android:layout_weight="1"

                ></TextView>

            <TextView
                android:id="@+id/IBUTitle"
                android:text="IBU"
                android:gravity="center"
                android:textStyle = "bold"
                android:textSize="10sp"
                android:layout_weight="1"

                ></TextView>

            <TextView
                android:id="@+id/glassTitle"
                android:text="Glass"
                android:gravity="center"
                android:textStyle = "bold"
                android:textSize="10sp"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                ></TextView>

            <TextView
                android:id="@+id/custRate"
                android:text="Avg Rating"
                android:gravity="center"
                android:textStyle = "bold"
                android:textSize="10sp"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                ></TextView>




        </TableRow>

        <TableRow
            android:id="@+id/tableStat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:padding="5dip" >

            <TextView
                android:id="@+id/abv"
                android:text=""
                android:gravity="center"
                android:textSize="15sp"
                android:layout_width="wrap_content"
                ></TextView>

            <TextView
                android:id="@+id/IBU"
                android:text=""
                android:gravity="center"
                android:textSize="15sp"
                android:layout_width="wrap_content"
                ></TextView>

            <TextView
                android:id="@+id/glass"
                android:text=""
                android:gravity="center"
                android:textSize="15sp"
                android:layout_width="wrap_content"
                ></TextView>

            <TextView
                android:id="@+id/beerRating"
                android:text=""
                android:gravity="center"
                android:textSize="15sp"
                android:layout_width="wrap_content"
                ></TextView>


        </TableRow>

    </TableLayout>


</LinearLayout >

更新小部件的异步任务:

public class GetRandomBeer extends AsyncTask
        <String, Void, String> {

    Context c;
    private RemoteViews views;

    public GetRandomBeer(Context context)
    {
        c = context;
        this.views = views;
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub







        return readJSONFeed(arg0[0]);
    }



    protected void onPostExecute(String result){
        Log.d("taste","Inside get taste");
        //decode json here
        try{

            //todo: get all beer data may need to do in doinbackground

            Log.d("widgetTest" , "inside async task");
            String beerText =  "try number 4";



            AppWidgetManager mgr = AppWidgetManager.getInstance(c);
            int[] appWidgetIds = mgr.getAppWidgetIds(new ComponentName(c, HelloWidget.class));

            //You need a static method somewhere (I usually put in widget provider class)
            //that builds the RemoteView and sets everything up.

            RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_test);


            views.setTextViewText(R.id.beerTitle, beerText);

            mgr.updateAppWidget(appWidgetIds, views);







        }
        catch(Exception e){

        }


    }

    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                inputStream.close();
            } else {
                Log.d("JSON", "Failed to download file");
            }
        } catch (Exception e) {
            Log.d("readJSONFeed", e.getLocalizedMessage());
        }
        return stringBuilder.toString();
    }

}

1 个答案:

答案 0 :(得分:1)

app小部件不支持TableLayout。