问题
I am trying to have a custom GLSurfaceView with some text over it, to display the score in a game. I have made a uniform xml layout based off of someones post here but when I try to load it with a setContentView the app crashes. after debugging I found that it says "Source can not be found". I have rebuilt the R file but that doesn't help. For reference my class that extends GLSurfaceView is called GLView. Any help would be appreciated.
package org.kizik.WLTBO;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class WholettheballoutActivity extends Activity {
GLView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score);
view = (GLView) findViewById(R.id.mySurfaceView);
// You can use a FrameLayout to hold the surface view
/*FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(view);
// Then create a layout to hold everything, for example a RelativeLayout
RelativeLayout relativeLayout= new RelativeLayout(this);
relativeLayout.addView(frameLayout);
relativeLayout.addView(score);
setContentView(relativeLayout);*/
}
@Override
protected void onPause() {
super.onPause();
view.onPause();
}
@Override
protected void onResume() {
super.onResume();
view.onResume();
}
}
With the XML file name score.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="fill_parent" >
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<org.kizik.WLTBO.GLView
android:id="@+id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
回答1:
It may happen if you have created a constructor with single parameter GLView(Context c) , you need to create another constructor like this GLView(Context c, AttributeSet attrs), so if u havent created that constructor, then it can not not find the GLView from class, coz its constructor is not created..!! i think you havent created that constructor..!!
public GLview(Context context, AttributeSet attrs){
super(context,attrs); }
回答2:
The problem is in your custom view's XML declaration, it should be something like this:
<org.kizik.WLTBO.GLView [other attributes]/>
来源:https://stackoverflow.com/questions/8598896/setcontentview-says-source-not-found